lundi 7 juin 2021

List of dictionaries to find required items [closed]

Assume I have people=

0  [{"voter": "Tom", "rshares": 10},{"voter": "Mark", "rshares": 5},{"voter": "Pam", "rshares": 7}]


1  [ {"voter": "Sam", "rshares": 7},{"voter": "Pam", "rshares": 7}]

and I also have a list of names=[Sam,Pam]. I'm wrtiting the code below to go over each row(0 and1) to see whether the name of voters are in the list of names (Sam and Pam) or not. If the names of voters are in the list then returun the aggregate rshares. Here it should return 7+7=14. But my code doesn't work.

for i in range(len(people)):
  test_list=people[i]
  res=None
  for sub in test_list:
      if sub['voter'].isin(names):
         res = sub['rshares']
   break

Aucun commentaire:

Enregistrer un commentaire