Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract substring from column

Tags:

python

I have a table where in one of the columns the data is a string that looks like this:

[{'label': 1, 'prob': 0.5001602182558444}, {'label': 0, 'prob': 0.49983978174415555}]

I want to extract just the number from label :1. So it would look like this:

0.5001602182558444

I can't seem to figure out how to do it with the standard methods since it has so many symbols which mess with the syntax.

like image 866
Ziggy Avatar asked Jun 26 '26 18:06

Ziggy


1 Answers

You could use a generator with next:

next((x['prob'] for x in lst if x['label'] == 1), None)

0.5001602182558444

Notice I add a None in case for no matches, if you're sure there would be matches you can strip that None:

next((x['prob'] for x in lst if x['label'] == 1))
like image 118
U12-Forward Avatar answered Jun 28 '26 07:06

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!