Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中构造一个循环_Python_Arrays_Cyclic - Fatal编程技术网

在python中构造一个循环

在python中构造一个循环,python,arrays,cyclic,Python,Arrays,Cyclic,我想在一个循环中循环一个列表。 例如:数组中有三个元素L=[1,2,3] 我想把输出作为 L[0],L[1] L[1],L[2] L[2],L[0] 有没有一种简单的方法可以得到稍微不同的输出 L[0],L[1] L[1],L[2] L[0],L[2]使用模运算符 >>> a = [1,2,3] >>> for x in range(10): print a[x % len(a)] 使用itertools.cycle >>>

我想在一个循环中循环一个列表。 例如:数组中有三个元素L=[1,2,3] 我想把输出作为

L[0],L[1]

L[1],L[2]

L[2],L[0]

有没有一种简单的方法可以得到稍微不同的输出

L[0],L[1]

L[1],L[2]

L[0],L[2]

使用模运算符

>>> a = [1,2,3]
>>> for x in range(10):
        print a[x % len(a)]
使用
itertools.cycle

>>> iterator = cycle(a)
>>> for _ in range(10):
        print next(iterator)
至于您的输出,您可以这样做

>>> for x in range(10):
        print '{0}, {1}'.format(a[x % len(a)], a[(x+1) % len(a)])

>>> 1, 2
>>> 2, 3
>>> 3, 1
... etc etc
利用模算子

>>> a = [1,2,3]
>>> for x in range(10):
        print a[x % len(a)]
使用
itertools.cycle

>>> iterator = cycle(a)
>>> for _ in range(10):
        print next(iterator)
至于您的输出,您可以这样做

>>> for x in range(10):
        print '{0}, {1}'.format(a[x % len(a)], a[(x+1) % len(a)])

>>> 1, 2
>>> 2, 3
>>> 3, 1
... etc etc

您可以只使用递增索引和模(除法的余数)

将产生:

(1,2)
(2,3)
(3,1)

您可以只使用递增索引和模(除法的余数)

将产生:

(1,2)
(2,3)
(3,1)

你可以试试类似的东西

L = [1,2,3]
length = len(L)
for i in xrange(length):
        print L[i % length], L[(i+1) % length]
输出

1 2
2 3
3 1
通过这种方式,您可以执行类似于
xrange(10)
的操作,但仍可以使其循环:

1 2
2 3
3 1
1 2
2 3
3 1
1 2
2 3
3 1
1 2

你可以试试类似的东西

L = [1,2,3]
length = len(L)
for i in xrange(length):
        print L[i % length], L[(i+1) % length]
输出

1 2
2 3
3 1
通过这种方式,您可以执行类似于
xrange(10)
的操作,但仍可以使其循环:

1 2
2 3
3 1
1 2
2 3
3 1
1 2
2 3
3 1
1 2
中有一个配方可供您使用:

import itertools
def pairwise(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return itertools.izip(a, b)
itertools.cycle(L)
调用此函数,您就可以开始了:

L = [1, 2, 3]
for pair in pairwise(itertools.cycle(L)):
    print pair
# (1, 2)
# (2, 3)
# (3, 1)
# (1, 2)
# ...
中有一个配方可供您使用:

import itertools
def pairwise(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return itertools.izip(a, b)
itertools.cycle(L)
调用此函数,您就可以开始了:

L = [1, 2, 3]
for pair in pairwise(itertools.cycle(L)):
    print pair
# (1, 2)
# (2, 3)
# (3, 1)
# (1, 2)
# ...

你是说组合

对于排序,您可以使用

sorted(comb, key=lambda x: x[1])
输出:

[(1, 2), (1, 3), (2, 3)]

你是说组合

对于排序,您可以使用

sorted(comb, key=lambda x: x[1])
输出:

[(1, 2), (1, 3), (2, 3)]

itertools.cycle
非常有趣!尽管来自“注意,工具箱的这个成员可能需要大量的辅助存储(取决于iterable的长度)。”但是
itertools.cycle
非常有趣!尽管来自“注意,工具箱的这个成员可能需要大量的辅助存储(取决于iterable的长度)。”反向复制只是因为另一篇文章有更好的答案。如果有争议,请在这里或聊天室给我打电话,因为对方的帖子有更好的答案。如果有争议,请在这里或聊天室告诉我