Skip to content Skip to sidebar Skip to footer

Implementing Ast.literal_eval On A Numpy Array

With the following expression, you can convert a string to a python dict. >>> import ast >>> a = ast.literal_eval('{'muffin' : 'lolz', 'foo' : 'kitty'}') >>

Solution 1:

Is this approximately what you're after?

import ast
importnumpya= numpy.array(["{'foo':123}","{'foo':234}"])
numpy.fromiter((ast.literal_eval(s)['foo'] for s in a), numpy.int_)

(Of course the appropriate dtype will depend on what's actually in those dicts.)

Post a Comment for "Implementing Ast.literal_eval On A Numpy Array"