当for循环有两个需要迭代的对象时,要用zip对这多个变量封装,否则会报错“too many values to unpack”

错误的例子:

    starts = [0,1,2,3,4]
    ends = [5,6,7,8,9]
    for start, end in starts, ends:
        print((start, end))

正确的例子:

    starts = [0,1,2,3,4]
    ends = [5,6,7,8,9]
    for start, end in zip(starts, ends):
        print((start, end))


 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐