在python中映射3个列表的值

2024-05-19 09:47:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三张单子

名称列表:brad, scott, bryan

相应年龄:20, 25, 23

他们各自喜欢的颜色:red, orange, green

当我在名称列表中循环时,我想得到年龄20和颜色red作为名称brad


Tags: 名称列表颜色greenredbryanscott单子
2条回答

你可以使用任意数量的序列

names = ['brad', 'scott', 'bryan']
colors = ['red', 'orange', 'green']
ages = [20, 25, 23]
for name, color, age in zip(names, colors, ages):
    print name, color, age
names = ['brad', 'scott', 'bryan']
colors = ['red', 'orange', 'green']
ages = [20, 25, 23]

for i, val in enumerate(names):
    print "{} {} {}".format(val, colors[i], ages[i])

相关问题 更多 >

    热门问题