1

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]
2
  • 1
    Your regex has two spaces after = but the input has only one space after =. Commented Jul 29, 2019 at 21:55
  • thanks , i am feeling so dumb now lol! Commented Jul 29, 2019 at 22:00

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

2 Comments

isn't there a way to do in this format re.findall(r"A = ([-+]?\d*\.\d+|\d+) dBm" , S)
@dd24 That will work to extract the float value, but you will need to use at least an S.split(' =')[0] or another re.findall to find the letter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.