python多个变量的for循环

  • Post author:
  • Post category:python


当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))



版权声明:本文为weixin_38314865原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。