Implementing ast.literal_eval on a numpy array

2024/11/19 7:47:49

With the following expression, you can convert a string to a python dict.

>>> import ast
>>> a = ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}")
>>> a
{'muffin': 'lolz', 'foo': 'kitty'}

And with the following expression, you can get the "foo" value:

>>> a.get('foo')

I have an array of strings which are similar to python dict format.

Firstly, I want to convert all of them to dict, so I will be able to create a dict array.

Secondly, I want to get all "foo" values from that dict array, so I will be able to create a "foo" array.

How can I manage this?

Thanks,

Answer

Is this approximately what you're after?

import ast
import numpy
a = 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.)

https://en.xdnf.cn/q/119975.html

Related Q&A

Best way to make argument parser accept absolute number and percentage?

I am trying to write a Nagios style check to use with Nagios. I have working script that takes in something like -w 15 -c 10 and interprets that as "Warning at 15%, Critical at 10%". But I ju…

Python calculating prime numbers

I have to define a function called is_prime that takes a number x as input, then for each number n from 2 to x - 1, test if x is evenly divisible by n. If it is, return False. If none of them are, then…

Why am I getting a column does not exist error when it does exist? I am modifying the Flask tutorial

I have a column named ticker_symbol, but I am getting a error when I run the error that there is no such column. Here is my auth.py code so far. It is similar to the Flask tutorial code. I get my get_d…

Update Key Value In Python In JSON File

How do I change a value in a json file with python? I want to search and find "class": "DepictionScreenshotsView" and replace it with "class": ""JSON File:{&quo…

Getting UnboundLocalError: local variable age referenced before assignment error

Ive written a simple script with TKinter and SQLAlchemy, where an sqlite .db is created, saving Employee information such as name, age, address, etc, etc.I realized that if a user puts a string in the …

Cannot run python script [duplicate]

This question already has answers here:What does "SyntaxError: Missing parentheses in call to print" mean in Python?(11 answers)Closed 9 years ago.I am trying to run this python script:https…

I want to read multiple audio files with librosa and then save it into an empty list

Here id my code . when I append into the array the array remain empty . Please help me where is the mistake. Or tell me some other way also to do thisA = [] # load more files with librosa pathAudio = …

How to db.execute in postgresql using the LIKE operator with variables within flask [duplicate]

This question already has an answer here:CS50: LIKE operator, variable substitution with % expansion(1 answer)Closed 4 years ago.Im trying to get my db.execute to work but encounter a syntax error when…

C, Perl, and Python similar loops different results

I wrote scripts to calculate pi in python, perl and c. They all use the same algorithm (trapezoidal reimann sum of a circle with n subintervals) and the python and perl programs always get the same re…

How to print a single backslash in python in a string? [duplicate]

This question already has answers here:Why do backslashes appear twice?(2 answers)Closed 1 year ago.In python (3x version)\ # this is error no doubt \\ # this prints two backslashes ,i.e., \\ r\ # thi…