【问题标题】:Sorting a list in Python without repeat previous item在Python中对列表进行排序而不重复上一项
【发布时间】:2014-07-23 09:11:17
【问题描述】:

我有一个独特的元素组合(A B C D E F)

from itertools import combinations

data = ['A', 'B', 'C', 'D', 'E', 'F'];
comb = combinations(data, 2);

d = [];
for i in comb:
    d.append([i[0], i[1]]);

print d

这回给我了:

[['A', 'B'], 
 ['A', 'C'], 
 ['A', 'D'],
 ['A', 'E'],
 ['A', 'F'],
 ['B', 'C'],
 ['B', 'D'],
 ['B', 'E'],
 ['B', 'F'],
 ['C', 'D'],
 ['C', 'E'],
 ['C', 'F'],
 ['D', 'E'],
 ['D', 'F'],
 ['E', 'F']]

问题是,如何以更简单的方式对第 N 行不重复第 (N-1) 行的元素 [0] 或元素 [1] 的方式进行排序:

AB (This line can have any element)
CD (This line can't have A or B)
EF (This line can't have C or D)
AC (This line can't have E or F)
...

【问题讨论】:

  • 你不需要在语句的末尾加上分号(你也不应该)。此外,您可以简单地使用d = map(list, combinations(data, 2)) 生成组合列表d
  • @JayanthKoushik 为什么不应该使用分号?谢谢:D
  • @sundarnatarajサンダーナタラジ 抱歉占用您的时间,我刚开始认为不可能按照我想要的方式组织...现在我正在尝试做一些数学计算来验证这一点...
  • @user3701822:你不应该使用分号,因为它没有任何用处并且违反了 python 风格的约定。它皱着眉头......
  • @JayanthKoushik 啊,我通常使用是因为两个原因,一个是因为 C 和 PHP 的习惯,另一个原因是因为有时我在同一行中编写多个东西......

标签: python arrays sorting loops organization


【解决方案1】:
mylist= [['A', 'B'],
 ['A', 'C'], 
 ['A', 'D'],
 ['A', 'E'],
 ['A', 'F'],
 ['B', 'C'],
 ['B', 'D'],
 ['B', 'E'],
 ['B', 'F'],
 ['C', 'D'],
 ['C', 'E'],
 ['C', 'F'],
 ['D', 'E'],
 ['D', 'F'],
 ['E', 'F']] 


a=mylist[:] #this used to assign all elements to a so u have ur mylist safe    
b=[]
b.append(a[0]) #this appends the first list in the list
del a[0]  #now deleting appended list        
while len(a)>0:
    for val,i in enumerate(a):# enumerte gives index and value of list
        if len(set(b[len(b)-1]).intersection(set(i)))==0: # this checks intersection so that both list should not have same elements
            b.append(a[val])
            del a[val]
print b

#output [['A', 'B'], ['C', 'D'], ['E', 'F'], ['A', 'C'], ['B', 'D'], ['C', 'E'], ['D', 'F'], ['A', 'E'], ['B', 'C'], ['D', 'E'], ['A', 'F'], ['B', 'E'], ['C', 'F'], ['A', 'D'], ['B', 'F']]

【讨论】:

  • 谢谢你!你解决了问题,我会研究你的代码。 :D
  • @user3701822 请注意,这会删除a中的条目
【解决方案2】:

使用来自this answer 的邻域生成器,您可以获得循环中的上一个、当前和下一个元素,以便您可以比较它们。然后你可以做这样的事情

from itertools import combinations

# Credit to Markus Jarderot for this function
def neighborhood(iterable):
    iterator = iter(iterable)
    prev = None
    item = iterator.next()  # throws StopIteration if empty.
    for next in iterator:
        yield (prev,item,next)
        prev = item
        item = next
        # this can be written like this also prev,item=item,next
    yield (prev,item,None)

data = ['A', 'B', 'C', 'D', 'E', 'F'];
comb = combinations(data, 2);

d = [];
for prev, item, next in neighborhood(comb):
    # If prev and item both exist and neither are in the last element in d
    if prev and item and not any(x in d[-1] for x in item):
        d.append([item[0], item[1]])
    elif item and not prev: # For the first element
        d.append([item[0], item[1]])

print d

打印出来

[['A', 'B'],
 ['C', 'D'],
 ['E', 'F']]

我知道这可能不是你需要的 100%,但它应该能够让你到达你想要的地方

【讨论】:

  • @sundarnatarajサンダーナタラジ 我知道你可以在我的最后一行看到。但是,即使它没有提供 OP 想要的完整代码,答案也会很有帮助和有用
  • @sundarnatarajサンダーナタラジ 没问题。你的回答太糟糕了+1你
  • @TimCastelijns 谢谢你,我正在阅读,这很有帮助:)
  • @user3701822 很高兴听到 ;-) 不要犹豫,为有用的答案投票,这也适用于 sundar 的答案
猜你喜欢
  • 2015-08-19
  • 2015-11-14
  • 2014-05-28
  • 2013-08-11
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
相关资源
最近更新 更多