1

I have a list of tuples each with 5 pieces of information in it. I need a way to search the list for a result or range of results from a search parameter or parameters. So I'd like to search for an ID number (string) or name (string - only the whole name) or a range of salary so between (int - 10000-20000). I read on another post How to search a list of tuples in Python that you can use list comprehensions

t = [[0] for x in l].index('12346')

but it wasn't explained all that well and it doesn't satisfy the range of values I need to enter. This does it's job for one entry. But if 2 people have the same first name or job it will just display the first occurrence. To display the correct tuple I take the value T and send it to the my printing function to display. Which again won't work for multiple entries because I would need to know how many tuples were returned beforehand.

Here are a few tuples, all are very similar.

('12349', '30000', 'Wizard', 'Harry', 'Potter')
('12350', '15000', 'Entertainer', 'Herschel Shmoikel', 'Krustofski')
('13123', '75000', 'Consultant', 'David', 'Beckham')
('13124', '150000', 'Manager', 'Alex', 'Ferguson')
('13125', '75000', 'Manager', 'Manuel', 'Pellegrini')

Thank you

Community
  • 1
  • 1
DonnellyOverflow
  • 3,981
  • 6
  • 27
  • 39

2 Answers2

0
t = [x for x in l if x[0] == '12346']
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0
>>> [e for e in l if int(e[1]) > 10000 and int(e[1]) < 20000]
[('12350', '15000', 'Entertainer', 'Herschel Shmoikel', 'Krustofski')]
Guy Gavriely
  • 11,228
  • 6
  • 27
  • 42