0

I got the following numpy array:

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']"])

and was wondering if anyone knows how to get the list of strings from there, eg. ['life', 'illusion', 'researcher',...]?

Thanks

rpanai
  • 12,515
  • 2
  • 42
  • 64
osterburg
  • 447
  • 5
  • 24

2 Answers2

3

Not quite sure why you have this string saved as a np.array, but you could just use ast.literal_eval:

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

literal_eval(a[0])
# ['life', 'illusion', 'researcher', 'prove', 'reality', 'doesnt...
yatu
  • 86,083
  • 12
  • 84
  • 139
2

You can split the string and then strip any extraneous characters:

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()]
iacob
  • 20,084
  • 6
  • 92
  • 119