如何获取 python numpy 数组中包含列表的任何元素的索引?
How can I get Index of any element in python numpy array that has list inside?
我有一个 numpy 数组,里面有列表。这是我的 numpy 数组结构
array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']), ...,
list(['af', 'ia']), list(['交換', 'sourc']),
list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
我用过的
np.where(Arr=="ia")
但我没有得到任何索引 return。这是我的输出
(array([], dtype=int64),)
如何像访问普通 numpy 数组一样访问元素索引?
提前致谢。
NumPy 仅适用于相同长度的数组和列表。
您能做的最好的事情是制作相同大小的所有列表并附加 None
元素,然后使用 NumPy 函数。使用这种方法的缺点是,如果有一个列表比其他列表大得多,填充的数组将使用大量内存来保存虚拟 None
值。
从this answer得到灵感,可以使用下面的代码
a = np.array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']),
list(['af', 'ia']), list(['交換', 'sourc']),
list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
max_length = max(len(l) for l in a)
filled_array = np.array([l + [None] * (max_length-len(l)) for l in a])
print(np.where(filled_array=="ia"))
In [213]: Arr=np.array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
...: list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠'])
...: ,
...: list(['af', 'ia']), list(['交換', 'sourc']),
...: list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
In [214]: Arr
Out[214]:
array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']),
list(['af', 'ia']), list(['交換', 'sourc']),
list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
使用常规 Python 列表方法:
In [215]: for i,l in enumerate(Arr):
...: print(i, 'ia' in l)
...:
0 False
1 False
2 False
3 True
4 False
5 False
In [216]: for i,l in enumerate(Arr):
...: if 'ia' in l:
...: print(i,l.index('ia'))
...:
3 1
对象 dtype 数组基本上是一个列表,几乎没有任何优势。 numpy
也没有太多自己的字符串方法。
我有一个 numpy 数组,里面有列表。这是我的 numpy 数组结构
array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']), ...,
list(['af', 'ia']), list(['交換', 'sourc']),
list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
我用过的
np.where(Arr=="ia")
但我没有得到任何索引 return。这是我的输出
(array([], dtype=int64),)
如何像访问普通 numpy 数组一样访问元素索引? 提前致谢。
NumPy 仅适用于相同长度的数组和列表。
您能做的最好的事情是制作相同大小的所有列表并附加 None
元素,然后使用 NumPy 函数。使用这种方法的缺点是,如果有一个列表比其他列表大得多,填充的数组将使用大量内存来保存虚拟 None
值。
从this answer得到灵感,可以使用下面的代码
a = np.array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']),
list(['af', 'ia']), list(['交換', 'sourc']),
list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
max_length = max(len(l) for l in a)
filled_array = np.array([l + [None] * (max_length-len(l)) for l in a])
print(np.where(filled_array=="ia"))
In [213]: Arr=np.array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
...: list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠'])
...: ,
...: list(['af', 'ia']), list(['交換', 'sourc']),
...: list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
In [214]: Arr
Out[214]:
array([list(['記', 'hk', '超抵', '玩', '轉台', '優惠', '無限', '任用']),
list(['学生', '个人', '兼职', '援交', '加']), list(['轉', '台大', '優惠']),
list(['af', 'ia']), list(['交換', 'sourc']),
list(['美食', 'cf', 'asm', '幾分'])], dtype=object)
使用常规 Python 列表方法:
In [215]: for i,l in enumerate(Arr):
...: print(i, 'ia' in l)
...:
0 False
1 False
2 False
3 True
4 False
5 False
In [216]: for i,l in enumerate(Arr):
...: if 'ia' in l:
...: print(i,l.index('ia'))
...:
3 1
对象 dtype 数组基本上是一个列表,几乎没有任何优势。 numpy
也没有太多自己的字符串方法。