python的for循环同时遍历两个list?

b = [1, 2, 3]
c = [(10, 20), (30, 40), (50, 60)]

for i, j in b, c:
    print(i, j)
ValueError: too many values to unpack (expected 2)
b = [1, 2]
c = [(10, 20), (30, 40)]

for i, j in b, c:
    print(j)
2
(30, 40)

这输出到底是怎么回事啊, 不能这么遍历吗...

为什么我印象中有这种用法呢...

dict.items() 就能用:

for key, value in x.items()

遍历..

阅读 4.7k
评论
    2 个回答

    好像没有这种用发吧,哈哈。

    但是我记得有这种用法:

    >>> a= 1
    >>> b= 2
    >>> a, b = b, a
    >>> print(a, b)
    2 1

    同时遍历的话,不知道下面这种能否解决?供参考:

    b = [1, 2, 3]
    c = [(10, 20), (30, 40), (50, 60)]
    for i in zip(b, c):
    print(i)
       
    >>>(1, (10, 20))
       (2, (30, 40))
       (3, (50, 60))
      相似问题
      推荐文章