Numpy 的 fromiter(~)
方法從可迭代對象(例如列表和字符串)構造 Numpy 數組。
參數
1. iterable
| iterable object
可迭代對象,例如列表和字符串。
2. dtype
| string
或 type
結果數組的數據類型。
3. count
| int
| optional
要從 iterable
讀取的值的數量。默認情況下, count=-1
,這意味著讀取所有值。
注意
指定count
以提高性能
通過指定 count,Numpy 可以預先創建固定大小的數組,而無需在迭代期間動態調整數組的大小。
返回值
一個 Numpy 數組。
例子
基本用法
要從 Python 列表創建 Numpy 數組:
x = [3,4,5]
np.fromiter(x, dtype=int)
array([3, 4, 5])
要從字節字符串創建 Numpy 數組:
x = "Cats"
np.fromiter(x, dtype="S1")
array([b'C', b'a', b't', b's'], dtype='|S1')
這裏,數據類型 S1
僅表示長度為 1
的字符串。
指定計數
僅考慮前兩項:
x = [3,4,5]
np.fromiter(x, dtype=int, count=2)
array([3, 4])
相關用法
- Python dict fromkeys()用法及代碼示例
- Python NumPy fromfunction方法用法及代碼示例
- Python NumPy frombuffer方法用法及代碼示例
- Python Django fromfile用法及代碼示例
- Python NumPy fromstring方法用法及代碼示例
- Python Django from_queryset用法及代碼示例
- Python Django fromstr用法及代碼示例
- Python frozenset()用法及代碼示例
- Python frexp()用法及代碼示例
- Python fractions.Fraction用法及代碼示例
- Python fractions.Fraction.limit_denominator用法及代碼示例
- Python fractions.Fraction.__floor__用法及代碼示例
- Python NumPy fliplr方法用法及代碼示例
- Python BeautifulSoup find_next方法用法及代碼示例
- Python functools.wraps用法及代碼示例
- Python NumPy floor方法用法及代碼示例
- Python functools.singledispatchmethod用法及代碼示例
- Python float轉exponential用法及代碼示例
- Python calendar firstweekday()用法及代碼示例
- Python NumPy full方法用法及代碼示例
- Python NumPy flatten方法用法及代碼示例
- Python float.is_integer用法及代碼示例
- Python Django format_lazy用法及代碼示例
- Python format()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | fromiter method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。