Reputation: 29
I have the following list of strings
List = ['A = -25.47 dBm' , 'B = -47.35 dBm' , 'A = -26.54 dBm' , 'B = -32.35 dBm', 'A = 27.95 dBm' , 'B = -64.11 dBm' , 'A = -45.11 dBm' , 'B = -18.67 dBm']
I want to extract the values of A make a list of that, values of B and make a list of that
I have tried the following
#First I joined the data into a string
S = ' '.join(List)
re.findall(r"A = ([-+]?\d*\.\d+|\d+) dBm" , S)
which doesn't seem to work
Expected Result
A_list = [-25.47,-26.54, 27.95,-45.11]
B_list = [-47.35,-32.35,-64.11,-18.67]
Upvotes: 1
Views: 60
Reputation: 71451
You can use re
and collections.defaultdict
:
import re, collections
l = ['A = -25.47 dBm' , 'B = -47.35 dBm' , 'A = -26.54 dBm' , 'B = -32.35 dBm', 'A = 27.95 dBm' , 'B = -64.11 dBm' , 'A = -45.11 dBm' , 'B = -18.67 dBm']
d = collections.defaultdict(list)
for i in l:
a, b = re.findall('^\w+|[\-\d\.]+', i)
d[a].append(float(b))
A_list, B_list = d['A'], d['B']
Output:
[-25.47, -26.54, 27.95, -45.11]
[-47.35, -32.35, -64.11, -18.67]
Upvotes: 1