简体   繁体   English

重复列表中的有序对

[英]Ordered Pair from repeating list

I have a list which may or may not have repeating elements, i wish to create ordered pairs such that in an element x is repeated the x,x is a valid pair other wise all x,y when x 我有一个可能包含或不包含重复元素的列表,我希望创建有序对,以便在元素x中重复x,x是有效对,否则x时所有x,y

so far i have found a very non - pythonic way of doing this 到目前为止,我已经找到了一种非常非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]]

i am aware i can use list comprehension but the solution i tried either skips the x,x type when repeats are present or and phantom x,x which should not be present 我知道我可以使用列表理解,但是我尝试过的解决方案要么在存在重复项时跳过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]]

what will be the appropriate pythonic solution for readability. 什么是适合可读性的pythonic解决方案。

Additionally what will the be most efficient solution time wise (memory is not a constraint) in case the pythonic solution is not the most efficient 另外,如果pythonic解决方案不是最有效的,那么在时间上最有效的解决方案(内存不是约束)

If you're happy using itertools, you can use itertools.combinations : 如果您对使用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)]

The inner call to sorted ensures that each pair will be ordered, which I understand was your intention. sorted的内部调用可确保每对都将被排序,据我了解,这是您的意图。 The outer call to sorted ensures that the pairs are ordered. sorted的外部调用可确保对有序。 If this is not necessary, you can replace this sorted with simply list . 如果这是没有必要的,可以更换这个sorted与简单的list

Have you tried using itertools.combinations() 您是否尝试过使用itertools.combinations()

Something like this 像这样

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

If you're not happy with itertools , you can use a list comprehension. 如果你快乐与itertools ,你可以用一个列表理解。

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

If you zip xs with all sublists of itself, starting at index 1, 2, ..., len(xs)-1, you get all the combinations: 如果使用其自身的所有子列表对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)]]

(Note that I wrapped here zip in a list for a nicer output.) Now, you have to flatten the list of tuples. (请注意,我在这里将zip包裹在一个list以获得更好的输出。)现在,您必须平整元组列表。 This is just a list comprehension with every tuple of every list of tuples yielded by zip : 这只是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)]

You want sorted tuples: this is tuple(sorted(t)) because sorted returns a list. 您需要排序的元组:这是tuple(sorted(t))因为sorted返回一个列表。 Put everything in a set to remove the duplicates. 将所有内容放在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)}

(You may also, as thesilkworm did, sort the list before you output the tuples.) Other test cases: (您也可以像蚕一样,在输出元组之前对列表进行排序。)其他测试用例:

>>> 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)}

Advice: you should use itertools ... 建议:您应该使用itertools ...

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

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