两个变量通过Python中的列表循环

时间:2016-08-02 22:44:22

标签: python loops

我有一个列表l = [100, 200, 300, 400, 500]

我想使用两个变量遍历它:

   for x, y in ???:
      print x, y

我希望得到如下结果:

100, 200
200, 300
300, 400
400, 500

我知道有很多方法可以做到,但我不确定哪一个优雅。如果您知道任何优雅方式,请告诉我。

1 个答案:

答案 0 :(得分:3)

优雅是相对的。使用zip

for x, y in zip(l, l[1:]):
    print x, y