如何查找某条数据在数组中的哪条记录?

How to find which record a specific piece of data is in an array?

我正在为我的 class 编写一个程序,我必须从用户那里获取输入数据并将该数据放入一个记录中,该记录又进入一个数组。

def createArray0fRecords(length):
  city_records = ["", "", 0.0,""]#Create record
  #City, Country, Population in Millions, Main Language

  city_array = [city_records]*length #Create Array of Records

  return city_array

def populateRecords(city_array):
  for counter in range (0, len(city_array)):
      print("")
      print("Please enter the city")
      city=input()

      print('Please enter the country')
      country=input()

      print("Please enter the population in millions")
      population=float(input())
      while population < 0:
        print(population," isn't a valid answer. Please input a number greater than 0.")
        population=input()
      print("Please enter the main language")
      language = input()

      city_array[counter] = [city, country, population, language]

  return city_array

def main_program():
  print("How many cities will you be entering?")
  length = int(input())
  city_array = createArray0fRecords(length)
  city_array = populateRecords(city_array)
  print("What city would you like the information about?")
  city=input()
  if city in (city_array[1]):
    print(city_array[city])

main_program()

我相信我快完成了,现在必须更改的只是最后几行。谢谢

抱歉,我是 stack overflow 的新手,我意识到我的问题措辞有误,我必须做的是将所有信息输入,然后我必须输入我想要获取有关信息的城市和该程序会给我关于那个城市的信息

您可以像这样按相关城市名称过滤 city_array (you can read on list comprehensions here)

relevant_cities = [c for c in city_array if c[0] == city]

relevant_cities 现在将成为城市与用户输入相同的所有记录的列表。您现在可以检查此列表是否为空,并根据需要执行任何操作。