gpt4 book ai didi

python - 搜索元组列表并显示结果

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:53 24 4
gpt4 key购买 nike

我有一个元组列表,我正在尝试搜索该列表并显示包含搜索词的所有结果。到目前为止我有这个:

holidays = []
holidays.append((1001,"Tenerife","Spain",2, 7, 80))
holidays.append((1002,"Cancun","Mexico",4, 14, 120))
holidays.append((1003,"Pairs","France",2, 3, 75))
holidays.append((1004,"Salzburg","Austria",3, 10, 90))
holidays.append((1004,"Madrid","Spain",3, 10, 90))enter code here

和:

search = input("Please enter a destination city or country to display all matching results: ")
for item in holidays:
if search in item:
print ()
print ("Here are all the results containing the word", search)
print ()
print ("Package ID:", item[0],":", item[1], "-", item[2],":",item[3], "person(s)",
"for", item[4], "nights costing" , "£" + str(item[5]), "pp/night")
print ()
menu = input("Press any key to return to the main menu:")

但是如果我搜索西类牙,它只会显示包含西类牙一词的第一个结果,而不是全部。我是否正确地认为它应该显示包含搜索词的所有结果,因为它位于 for 循环内?

最佳答案

就这样做

print ()
print ("Here are all the results containing the word", search)
print ()
for item in holidays:
if search in item:
print ("Package ID:", item[0],":", item[1], "-", item[2],":",item[3], "person(s)",
"for", item[4], "nights costing" , "£" + str(item[5]), "pp/night")
print ()
menu = input("Press any key to return to the main menu:")

循环搜索完成后会提示输入。

关于python - 搜索元组列表并显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53469776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com