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
  • 8,748
  • 2
  • 24
  • 47
osterburg
  • 357
  • 3
  • 14

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
  • 75,195
  • 11
  • 47
  • 89
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
  • 7,935
  • 4
  • 26
  • 52