【问题标题】:Iterate through 2 lists at once in Python在 Python 中一次遍历 2 个列表
【发布时间】:2017-04-12 06:48:27
【问题描述】:

我想向两个 Python 列表的成员共享/分配特定数量。 list1 的成员各有双份,list2 的成员各有一份,然后打印出结果,并分配名称和值。以下是我的示例代码:

reminder = float(85000)

list1 = ['Designer', 'Coder', 'Supervisor']
list2 = ['Artist', 'Attendant', 'Usher]

for i,j in list1 and list2:
    print(i, (reminder/9)*2)
    print(j, (reminder/9)*1)

当我运行上面的代码时,我得到了一个异常:

Traceback (most recent call last):
  File "C:\Python33\reminder.py", line 6, in <module>
    for i,j in list1 and list2:
ValueError: too many values to unpack (expected 2)

我搜索了有关同一主题的先前帖子,但找不到解决方案。

请问我该怎么做?

【问题讨论】:

标签: python list iteration


【解决方案1】:

你应该使用zip()内置函数。

for i, j in zip(list1, list2):
    print(i, (reminder/9)*2)
    print(j, (reminder/9)*1)

【讨论】:

  • 感谢您的及时回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 2011-03-18
  • 2018-11-26
  • 1970-01-01
  • 2011-04-26
相关资源
最近更新 更多