【问题标题】:Can i integrate multiple lists in python? [duplicate]我可以在 python 中集成多个列表吗? [复制]
【发布时间】:2012-07-12 11:02:36
【问题描述】:

可能重复:
how can I iterate through two lists in parallel in Python?

我有 3 个这样的列表:

name = ['sam', 'john', 'napoleon', 'tanaka', 'ming']
country = ['USA', 'England', 'France', 'Japan', 'China']
age = [23, 41, 19, 16, 55]

我想要这样的输出:

sam; USA; 23
john; England; 41
napoleon; France; 19
tanaka; Japan; 16
ming; China; 55

任何帮助都会很棒。 提前致谢

【问题讨论】:

    标签: python list


    【解决方案1】:

    如果您知道它们都具有相同的长度,那么您可以这样做:

    name = ['sam', 'john', 'napoleon', 'tanaka', 'ming']
    country = ['USA', 'England', 'France', 'Japan', 'China']
    age = [23, 41, 19, 16, 55]
    
    i = 0
    
    for n in name:
        print name[i] + '; ' + country[i] + '; ' + str(age[i])
        i++
    

    【讨论】:

    • 最好使用xrange(len(name)),而不是手动增加i
    • 非常正确,我认为 D Seitas 的回答是我最喜欢的方式
    • ...或使用for i, n in enumerate(name): ...
    • i++ ? python 2.x 没有 ++ 运算符
    【解决方案2】:

    我的 2 美分 ;-)

    使用来自 itertools 模块的生成器:

    from itertools import izip
    
    nlist = ['%s; %s; %s' %t for t in izip(name, country, age)]
    

    【讨论】:

    • 两个问题 - 缺少右方括号...并且您无法将 int 连接到字符串 :)
    • 确实如此 - 您应该始终尝试运行自己的代码 ;-)
    【解决方案3】:

    假设列表长度相同:

    for i in range(len(name)):
        print '%s; %s; %s' %(name[i], country[i], age[i])
    

    更新:假设您使用的是 Python 3.x。对于 2.x,使用 xrange 进行一般性能改进。或者:

    for i in range(len(name)):
        print '{0}; {1}; {2}'.format{name[i], country[i], age[i]}
    

    【讨论】:

    • range(len(name)) 很贵。
    • @jakob:你需要付钱吗? <:p>
    • @DSeita 在 python 2.x 上使用 xrange(),在 python 3.x 上使用 range() 并使用 format() 格式化字符串而不是旧式 (%s) 格式化。
    • range(len(name)) 仍然是个坏主意!,就做for pos, i in enumerate(name)
    【解决方案4】:
    name = "sam john napoleon tanaka ming".split()
    country = "USA England France Japan China".split()
    age = [23, 41, 19, 16, 55]
    
    print "\n".join(["%s; %s %d" % (x) for x in zip(name,country,age)])
    

    【讨论】:

    • +1 表示一个班轮,使用format() 而不是这些旧的%s 格式。
    • @AshwiniChaudhary 实际上 %s 仍然完全可信
    • @JakobBowyer 但最好使用format(),因为旧式格式可能会被弃用。
    • @AshwiniChaudhary 可能是语言设计中的一件大事
    • @JakobBowyer PEP 3101:高级字符串格式。注意:2.6描述mentions the format() method for both 8-bit and Unicode strings. In 3.0, only the str type (text strings with Unicode support) supports this method; the bytes type does not. The plan is to eventually make this the only API for string formatting, and to start deprecating the % operator in Python 3.1.来源:What’s New In Python 3.0,所以建议使用format更好。
    【解决方案5】:

    你可以使用zip():

    name = ['sam', 'john', 'napoleon', 'tanaka', 'ming']
    country = ['USA', 'England', 'France', 'Japan', 'China']
    age = [23, 41, 19, 16, 55]
    for x,y,z in zip(name,country,age):
        print("{0}; {1}; {2}".format(x,y,z))
    

    或使用map():

    mapp=map(lambda x,y,z:"; ".join((x,y,str(z))),name,country,age)
    for x in mapp:
        print(x)
    

    输出:

    sam; USA; 23
    john; England; 41
    napoleon; France; 19
    tanaka; Japan; 16
    ming; China; 55
    

    【讨论】:

    • 我有一个答案,但这个更好。感谢您提供有关 zip 的提示!
    • 谢谢!它真的有帮助!很好的解决方案
    • 可能更整洁:map("; ".join, names, countries, map(str, ages))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    相关资源
    最近更新 更多