I want to extract some information form a list of sets . I know that I have to use python module re but i will be happy to have some help. This is the list of sets:
[{'group_id': 'E1', 'subjects_affected': '4', 'subjects_at_risk': '104'},
{'group_id': 'E2', 'subjects_affected': '7', 'subjects_at_risk': '105'}]
I want to extract information related to subjects_affected in each set. So I need to have :['4','7']
Thank you,
import re
b=[{'group_id': 'E1', 'subjects_affected': '0', 'subjects_at_risk': '104'},
{'group_id': 'E2', 'subjects_affected': '0', 'subjects_at_risk': '105'}]
re.findall(r'b\{\aff \b', b)
CodePudding user response:
You don't need re for this case:
c = [dic['subjects_affected'] for dic in b]
CodePudding user response:
You can use a list comprehension:
>>> b=[{'group_id': 'E1', 'subjects_affected': '0', 'subjects_at_risk': '104'},
... {'group_id': 'E2', 'subjects_affected': '0', 'subjects_at_risk': '105'}]
>>> [x['subjects_affected'] for x in b]
['0', '0']