首页 > 解决方案 > 使用另一个列表的索引值打印列表中的项目?

问题描述

我有两份清单,一份列出目的地,另一份列出您前往上述地点所需的费用。

例如,我需要做的是,如果用户选择"7500"了所有费用"7500"将显示的地方。正如您在下面的代码中看到的那样"7500",重复了两次,所以我需要两个具有该价格的目的地。

我已经有了找到所需点的代码,但我不知道如何继续打印与这些点相关的目的地。

destinations = ["Toronto", "Winnipeg", "London", "Ottawa","Miami", "Edmonton"]
pointCosts = [7500, 9000, 11000, 7500, 9500, 9000]

def CheapPoint (pointCosts):
    lowest = [0]
    for x in pointCosts:
        if x < lowest:
            lowest = x

例如,对于输出,我想要这样的东西:

Points: 7500 City: Toronto City: Ottawa

截至目前我只得到积分,但我也想得到目的地,我也不能使用任何内置函数。

谢谢

标签: pythonpython-3.xlist

解决方案


score = 7500

example = [ x for x, y in zip(destinations, pointCosts) if y == score ]

输出

['Toronto', 'Ottawa']

您可以通过 print() 函数在单独的行上打印:

print(*example, sep = '\n')

输出:

Toronto
Ottawa

推荐阅读