繁体   English   中英

重复列表中的有序对

[英]Ordered Pair from repeating list

我有一个可能包含或不包含重复元素的列表,我希望创建有序对,以便在元素x中重复x,x是有效对,否则x时所有x,y

到目前为止,我已经找到了一种非常非pythonic的方法

def foo(a):
    n = list()
    for x in range(len(a)):
        for y in range(x+1,len(a)):
            if a[x] < a[y]:
                n.append([a[x],a[y]])
            else:
                n.append([a[y],a[x]])
    o = list()
    for p in n:
        if not (p in o):
            o.append(p)
    return o

print(foo([1,3,5,-1]))
# [[1, 3], [1, 5], [-1, 1], [3, 5], [-1, 3], [-1, 5]]

print(foo([1,1,5,5]))
# [[1, 1], [1, 5], [5, 5]]

print(foo([1,1,1,1]))
# [[1, 1]]

我知道我可以使用列表理解,但是我尝试过的解决方案要么在存在重复项时跳过x,x类型,要么不应该存在幻像x,x

a = [1,3,5,-1]
o = [[x,y] for x in a for y in a if x<=y]
print(o)
[[1, 1], [1, 3], [1, 5], [3, 3], [3, 5], [5, 5], [-1, 1], [-1, 3], [-1, 5], [-1, -1]]

什么是适合可读性的pythonic解决方案。

另外,如果pythonic解决方案不是最有效的,那么在时间上最有效的解决方案(内存不是约束)

如果您对使用itertools感到满意,可以使用itertools.combinations

from itertools import combinations

a = [1, 3, 5, -1]
o = sorted(set(combinations(sorted(a), 2)))

>>> [(-1, 1), (-1, 3), (-1, 5), (1, 3), (1, 5), (3, 5)]

a = [1, 1, 5, 5]
o = sorted(set(combinations(sorted(a), 2)))

>>> [(1, 1), (1, 5), (5, 5)]

sorted的内部调用可确保每对都将被排序,据我了解,这是您的意图。 sorted的外部调用可确保对有序。 如果这是没有必要的,可以更换这个sorted与简单的list

您是否尝试过使用itertools.combinations()

像这样

import itertools
iterable = [1,1,5,5]
r=2
comb = sorted(set(itertools.combinations(sorted(iterable), r)))
for el in comb:
  print(el)

如果你快乐与itertools ,你可以用一个列表理解。

>>> xs = [1,3,5,-1]

如果使用其自身的所有子列表对xs进行压缩,从索引1、2 ... ... len(xs)-1开始,则会得到所有组合:

>>> [list(zip(xs,xs[n:])) for n in range(1,len(xs))]
[[(1, 3), (3, 5), (5, -1)], [(1, 5), (3, -1)], [(1, -1)]]

(请注意,我在这里将zip包裹在一个list以获得更好的输出。)现在,您必须平整元组列表。 这只是zip产生的每个元组列表中的每个元组的列表理解:

>>> [t for ys in [zip(xs,xs[n:]) for n in range(1,len(xs))] for t in ys]
[(1, 3), (3, 5), (5, -1), (1, 5), (3, -1), (1, -1)]

您需要排序的元组:这是tuple(sorted(t))因为sorted返回一个列表。 将所有内容放在set以删除重复项。

>>> set(tuple(sorted(t)) for ys in [zip(xs,xs[n:]) for n in range(1,len(xs))] for t in ys)
{(-1, 1), (1, 3), (-1, 3), (1, 5), (-1, 5), (3, 5)}

(您也可以像蚕一样,在输出元组之前对列表进行排序。)其他测试用例:

>>> xs = [1,1,1,1]
>>> set(tuple(sorted(t)) for ys in [zip(xs,xs[n:]) for n in range(1,len(xs))] for t in ys)
{(1, 1)}
>>> xs = [1,1,5,5]
>>> set(tuple(sorted(t)) for ys in [zip(xs,xs[n:]) for n in range(1,len(xs))] for t in ys)
{(1, 5), (5, 5), (1, 1)}

建议:您应该使用itertools ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM