Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何比较同一列表中的元组?_Python_List_Function_Tuples - Fatal编程技术网

Python 如何比较同一列表中的元组?

Python 如何比较同一列表中的元组?,python,list,function,tuples,Python,List,Function,Tuples,例如,如果我创建了一个函数,该函数接收列表“people”和列表中的两个名字作为输入,以比较这些人是否出生在同一个地方,我如何在不重复自己的情况下进行比较 people = [ ('Edsger Dijkstra', (1930, 5, 11), 'Holland'), ('Alan Turing', (1912, 6, 23), 'England'), ('Alonzo Church', (1903, 6, 14), 'United States'), ('Stephe

例如,如果我创建了一个函数,该函数接收列表“people”和列表中的两个名字作为输入,以比较这些人是否出生在同一个地方,我如何在不重复自己的情况下进行比较

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')
]

最简单的方法可能是在列表中迭代一次,并在运行时挑选出与输入匹配的元素的元组。找到匹配的元组后,将感兴趣的元素提取到函数中定义的变量,然后继续。最后,比较两个内部变量的设置,这将告诉您两个输入是否具有匹配的对应值

以下是您的示例,以这种方式演示:

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))
        
        

解决方案1:您可以通过键入名称来搜索匹配项:

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)
解决方案2:通过菜单选择进行搜索:

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)
人=[
(《埃德格·迪克斯特拉》,(1930,5,11),《荷兰》),
(《艾伦·图灵》(1912,6,23),《英格兰》),
(《阿隆佐教堂》(1903,6,14),《美国》),
(《斯蒂芬·库克》(1939、12、14)、《美国》),
('Guido van Rossum',(1956年1月31日),'Holland'),
(《托尼·霍尔》,(1934,1,11),《英格兰》),
(《格蕾丝·霍珀》(1906,12,9),《美国》),
(《查尔斯·巴贝奇》(1791、12、26)、《英格兰》),
(《唐纳德·克努斯》,(1938,1,10),《美国》)
]
def checkLocationMatch(人员):
退出=错误
不退出时:
打印(“人员列表:”)
打印(“-----------\n”)
对于范围内的i(len(people)):
打印(“%d)%s”%(i+1,人员[i][0]))
退出人数=人数(人)+1
打印(“%d)退出“%EXIT\u num”)
index1=int(输入(“\n输入人员1号:”)
如果index1==退出数量:
打破
elif index1<1或index1>(len(people)-1):
打印(“\n个人1号超出范围\n”)
持续
index2=int(输入(“输入人员2编号:”)
如果index2==退出数量:
打破
elif index2<1或index2>(人员-1):
打印(“\n第2个数字超出范围\n”)
持续
如果people[index1-1][2]==people[index2-1][2]:
打印(“位置匹配。%s\n”%people[index1-1][2])
其他:
打印(“位置不匹配。(%s和%s)\n”%(人员[index1-1][2],人员[index2-1][2]))
checkLocationMatch(人员)

您尝试过什么?我们不能只为你工作。