fromiter Must specify length when using variable-size data-type.
时间: 2024-05-10 19:13:59 浏览: 234
linux mount报错:you must specify the filesystem type的解决方法
The error message "Must specify length when using variable-size data-type" is raised when using the `numpy.fromiter` function and the data-type of the array being generated is variable-length. This means that the length of each element in the array is not fixed or predetermined.
To resolve this error, you need to specify the length of the data-type explicitly. For example, if you want to create an array of strings with variable lengths, you can specify the data-type as `'U'` followed by the maximum length of the string. Here's an example:
```
import numpy as np
# create an iterator of strings with variable lengths
iterable = ('hello', 'world!', 'numpy', 'fromiter')
# specify the data-type with variable-length strings
dtype = 'U10'
# create the array from the iterator
arr = np.fromiter(iterable, dtype=dtype)
print(arr)
```
Output:
```
['hello' 'world!' 'numpy' 'fromiter']
```
In this example, the data-type `'U10'` specifies that each element in the array is a Unicode string with maximum length of 10 characters.
阅读全文