并且如果已经完成

并且如果已经完成

本文介绍了如何在Python中运行i< j循环,并且如果已经完成(i,j),则不重复(j,i)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现"i不等于j".( i< j )循环,它跳过了 i = j 的情况,但我还要提出另外的要求,即循环不得重复的排列>(j,i),如果已经完成了(i,j)(由于对称,这两种情况给出了相同的解决方案).

I am trying to implement an "i not equal to j" (i<j) loop, which skips cases where i = j, but I would further like to make the additional requirement that the loop does not repeat the permutation of (j,i), if (i,j) has already been done (since, due to symmetry, these two cases give the same solution).

在后面的代码中,我通过遍历以下列表来创建 i< j 循环,其中第二个列表只是前一个滚动的第一个列表:

In the code to follow, I make the i<j loop by iterating through the following lists, where the second list is just the first list rolled ahead 1:

mylist = ['a', 'b', 'c']
np.roll(mylist,2).tolist() = ['b', 'c', 'a']

以下代码生成的序列原来不是我想要的:

The sequence generated by the code below turns out to not be what I want:

import numpy as np

mylist = ['a', 'b', 'c']
for i in mylist:
    for j in np.roll(mylist,2).tolist():
        print(i,j)

因为它返回重复的 a a 并具有重复的排列 a b b a :

since it returns a duplicate a a and has repeated permutations a b and b a:

a b
a c
a a
b b
b c
b a
c b
c c
c a

相反,所需的顺序应该是 mylist 中元素的成对组合,因为对于 N = 3 元素,应该只存在 N *(N-1)/2 = 3 对可循环通过:

The desired sequence should instead be the pair-wise combinations of the elements in mylist, since for N=3 elements, there should only be N*(N-1)/2 = 3 pairs to loop through:

a b
a c
b c

推荐答案

您可以使用 list.insert 帮助左移和右移.

You can use list.insert to help with left shift and right shift.

list.pop ,将元素从原始列表中删除并返回. list.insert 将返回的元素以给定索引(在这种情况下为0或-1)添加到列表中.注意:此操作到位!

list.pop, removes the element from the original list and returns it as well. list.insert adds the returned element into the list at given index (0 or -1 in this case). NOTE: this operation is in place!

#Left shift
mylist = ['apples', 'guitar', 'shirt']
mylist.insert(-1,mylist.pop(0))
mylist

### ['guitar', 'apples', 'shirt']
#Right shift
mylist = ['apples', 'guitar', 'shirt']
mylist.insert(0,mylist.pop(-1))
mylist

### ['shirt', 'apples', 'guitar']


更好的方法是使用 collections.deque .这将使您可以进行多个班次工作,并且还可以使用其他一些简单的队列功能.


A better way to do this is with collections.deque. This will allow you to work with multiple shifts and has some other neat queue functions available as well.

from collections import deque

mylist = ['apples', 'guitar', 'shirt']
q = deque(mylist)
q.rotate(1)       # Right shift ['shirt', 'apples', 'guitar']
q.rotate(-1)      # Left shift ['guitar', 'shirt', 'apples']
q.rotate(3)       #Right shift of 3 ['apples', 'guitar', 'shirt']


编辑:根据您的评论,您尝试获取排列-


Based on your comments, you are trying to get permutations -

from itertools import product
[i for i in product(l, repeat=2) if len(set(i))>1]
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]

OR

out = []
for i in l:
    for j in l:
        if len(set([i,j]))>1:
               print(i,j)
a b
a c
b a
b c
c a
c b

这篇关于如何在Python中运行i&lt; j循环,并且如果已经完成(i,j),则不重复(j,i)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:44