如何从numpy数组中提取列表?

2024-09-28 17:07:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了以下numpy数组:

array(["['life', 'illusion', 'researcher', 'prove', 'reality', 'doesnt', 'exist', 'youre', 'looking', 'life', 'illusion', 'least', 'quantum', 'level', 'theory', 'recently', 'confirmed', 'set', 'researcher', 'finally', 'mean', 'test', 'john', 'wheeler’s', 'delayedchoice', 'theory', 'concluded', 'physicist', 'right', '1978', 'mr', 'wheeler’s', 'proposed', 'experiment', 'involved', 'moving', 'object', 'given', 'choice', 'act', 'like', 'wave', 'particle', 'former', 'acting', 'vibration', 'frequency', 'distinguish', 'wave', 'latter', 'frequency', 'determine', 'position', 'space', 'unlike', 'wave', 'point', '‘decide’', 'act', 'like', 'one', 'time', 'technology', 'available', 'conduct', 'strong', 'experiment', 'scientist', 'able', 'carry']"])

我想知道是否有人知道如何从那里得到字符串列表,例如['life', 'illusion', 'researcher',...]?你知道吗

谢谢


Tags: numpy数组wavearrayactwheelerlikeexperiment
2条回答

您可以拆分字符串,然后删除任何无关字符:

x = np.array(["['life', 'illusion', 'researcher', 'prove', 'reality', 'doesnt', 'exist', 'youre', 'looking', 'life', 'illusion', 'least', 'quantum', 'level', 'theory', 'recently', 'confirmed', 'set', 'researcher', 'finally', 'mean', 'test', 'john', 'wheeler’s', 'delayedchoice', 'theory', 'concluded', 'physicist', 'right', '1978', 'mr', 'wheeler’s', 'proposed', 'experiment', 'involved', 'moving', 'object', 'given', 'choice', 'act', 'like', 'wave', 'particle', 'former', 'acting', 'vibration', 'frequency', 'distinguish', 'wave', 'latter', 'frequency', 'determine', 'position', 'space', 'unlike', 'wave', 'point', '‘decide’', 'act', 'like', 'one', 'time', 'technology', 'available', 'conduct', 'strong', 'experiment', 'scientist', 'able', 'carry']"])

[x.strip("[]'',") for x in x[0].split()]

不太清楚为什么要将此字符串保存为np.array,但可以使用^{}

from ast import literal_eval
a = np.array(["['life', 'illusion', 'researcher', 'prove', 'reality..."])

literal_eval(a[0])
# ['life', 'illusion', 'researcher', 'prove', 'reality', 'doesnt...

相关问题 更多 >