python - 使用另一个列表的索引值打印列表中的项目?
问题描述
我有两份清单,一份列出目的地,另一份列出您前往上述地点所需的费用。
例如,我需要做的是,如果用户选择"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
截至目前我只得到积分,但我也想得到目的地,我也不能使用任何内置函数。
谢谢
解决方案
score = 7500
example = [ x for x, y in zip(destinations, pointCosts) if y == score ]
输出
['Toronto', 'Ottawa']
您可以通过 print() 函数在单独的行上打印:
print(*example, sep = '\n')
输出:
Toronto
Ottawa
推荐阅读
- ios - 一个 pod 找不到另一个 pod lib 中的 .h fie
- drupal - 在 Consumers Drupal 8 模块中获取“consumer__roles 表不存在:SELECT t.* FROM”
- unity3d - 如何将 float3 打包成一个 float
- android - 如何在不重复前一个值的情况下创建随机列表?
- flutter - Flutter:显示分钟和秒的倒数计时器
- ios - NSAttributedString 绘图 - 多少行?
- .net - 在 EF Core 2.0 中配置 UseIdentityColumn
- python-3.x - Python:如何在函数外使用函数中定义的图形?
- c# - Uno 平台:字体在 Android 上无法正确显示
- python-3.x - 我应该堆叠、旋转还是分组?