我想在我的numpy字符串数组中执行此操作
>>> set('Hello world')
{' ', 'H', 'd', 'e', 'l', 'o', 'r', 'w'}
这就是我所做的
>>> x=np.array([[' X-ray is dangerous'],['Zebra crosses the road'],['I Love XYZ']])
>>>set(x.flatten())
{' X-ray is dangerous', 'I Love XYZ', 'Zebra crosses the road'}
>>> np.unique(x)
array([' X-ray is dangerous', 'I Love XYZ', 'Zebra crosses the road'],
dtype='|S22')
看到它没有给出所需的输出,我想得到整个numpy数组的设定值而不仅仅是行。我该怎么办?
答案 0 :(得分:1)
>>> set("".join(x.flatten()))
{'r', ' ', 'g', 'y', 'h', 'I', 'd', '-', 'a', 'i', 'n', 'Z', 's', 'o', 't', 'b', 'v', 'c', 'e', 'Y', 'X', 'u', 'L'}
或者如果你担心非常大的numpy
数组:
>>> data = set()
>>> for row in x.flatten():
... data.update(row)
...
>>>
# {'r', ' ', 'g', 'y', 'h', 'I', 'd', '-', 'a', 'i', 'n', 'Z', 's', 'o', 't', 'b', 'v', 'c', 'e', 'Y', 'X', 'u', 'L'}