For example, if I create a function that receives as input the list "people" and also two names from the list to compare if those people were born in the same place, how can I make this comparison without repeating themselves?
people = [
('Edsger Dijkstra', (1930, 5, 11), 'Holland'),
('Alan Turing', (1912, 6, 23), 'England'),
('Alonzo Church', (1903, 6, 14), 'United States'),
('Stephen Cook', (1939, 12, 14), 'United States'),
('Guido van Rossum', (1956, 1, 31), 'Holland'),
('Tony Hoare', (1934, 1, 11), 'England'),
('Grace Hopper', (1906, 12, 9), 'United States'),
('Charles Babbage', (1791, 12, 26), 'England'),
('Donald Knuth', (1938, 1, 10), 'United States')
]
Simplest way might be to iterate through the list once and pick out tuples with elements that match your inputs as you go. When you find a tuple that matches, extract the element of interest to a variable defined within the function and then continue on. At the end, compare what your two internal variables have been set to and that will tell you if the two inputs have matching corresponding values.
Here's your example played out in this manner:
people = [
('Edsger Dijkstra', (1930, 5, 11), 'Holland'),
('Alan Turing', (1912, 6, 23), 'England'),
('Alonzo Church', (1903, 6, 14), 'United States'),
('Stephen Cook', (1939, 12, 14), 'United States'),
('Guido van Rossum', (1956, 1, 31), 'Holland'),
('Tony Hoare', (1934, 1, 11), 'England'),
('Grace Hopper', (1906, 12, 9), 'United States'),
('Charles Babbage', (1791, 12, 26), 'England'),
('Donald Knuth', (1938, 1, 10), 'United States')
]
def finder(lst, person_1, person_2):
person_1_birthplace = 0
person_2_birthplace = 1
for i in lst:
if i[0] == person_1:
person_1_birthplace = i[2]
if i[0] == person_2:
person_2_birthplace = i[2]
if person_1_birthplace == person_2_birthplace:
print('Both {person1} and {person2} were born in {birthplace}.'.format(person1 = person_1, person2 = person_2, birthplace = person_1_birthplace))
else:
print('{person1} and {person2} were born in different places.'.format(person1 = person_1, person2 = person_2, birthplace = person_1_birthplace))
Solution 1: You can search for matches by typing in a name:
people = [
('Edger Dijkstra', (1930, 5, 11), 'Holland'),
('Alan Turing', (1912, 6, 23), 'England'),
('Alonzo Church', (1903, 6, 14), 'United States'),
('Stephen Cook', (1939, 12, 14), 'United States'),
('Guido van Rossum', (1956, 1, 31), 'Holland'),
('Tony Hoare', (1934, 1, 11), 'England'),
('Grace Hopper', (1906, 12, 9), 'United States'),
('Charles Babbage', (1791, 12, 26), 'England'),
('Donald Knuth', (1938, 1, 10), 'United States')
]
def checkLocationMatch(people):
print("People List:")
print("-------------\n")
for i in range(len(people)):
print(people[i][0])
name1 = input("\nEnter name 1: ")
name2 = input("Enter name 2: ")
city1 = None
city2 = None
name1_exists = False
name2_exists = False
for person in people:
if person[0].lower() == name1.lower():
city1 = person[2]
name1_exists = True
elif person[0].lower() == name2.lower():
city2 = person[2]
name2_exists = True
if not name1_exists:
print("Name 1 doesn't exist")
if not name2_exists:
print("Name 2 doesn't exist")
if name1_exists and name2_exists:
if city1 == city2:
print("\nLocation match (%s)" % city1)
else:
print("\nLocation's don't match. (%s and %s)" % (city1, city2))
checkLocationMatch(people)
Solution 2: Searching by menu selection:
people = [
('Edger Dijkstra', (1930, 5, 11), 'Holland'),
('Alan Turing', (1912, 6, 23), 'England'),
('Alonzo Church', (1903, 6, 14), 'United States'),
('Stephen Cook', (1939, 12, 14), 'United States'),
('Guido van Rossum', (1956, 1, 31), 'Holland'),
('Tony Hoare', (1934, 1, 11), 'England'),
('Grace Hopper', (1906, 12, 9), 'United States'),
('Charles Babbage', (1791, 12, 26), 'England'),
('Donald Knuth', (1938, 1, 10), 'United States')
]
def checkLocationMatch(people):
exit = False
while not exit:
print("People List:")
print("-------------\n")
for i in range(len(people)):
print("%d) %s" % (i + 1, people[i][0]))
exit_num = len(people) + 1
print("%d) EXIT" % exit_num)
index1 = int(input("\nEnter person 1 number: "))
if index1 == exit_num:
break
elif index1 < 1 or index1 > (len(people) - 1):
print("\nPerson 1 number out of bounds\n")
continue
index2 = int(input("Enter person 2 number: "))
if index2 == exit_num:
break
elif index2 < 1 or index2 > (len(people) - 1):
print("\nPerson 2 number out of bounds\n")
continue
if people[index1 - 1][2] == people[index2 - 1][2]:
print("Location Match. %s\n" % people[index1 - 1][2])
else:
print("Location's don't Match. (%s and %s)\n" % (people[index1 - 1][2], people[index2 - 1][2]))
checkLocationMatch(people)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.