0

Given pairs of items of form [(a,b),...] where (a,b) means a > b, for example:

[('best','better'),('best','good'),('better','good')] I would like to output a list of form:

['best','better','good'] This is very hard for some reason. Any thoughts?

xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
  • you just want the unique elements from the given list, right? – Wasi Ahmad Dec 12 '16 at 00:31
  • You'd like to flatten the list? – Christian Dean Dec 12 '16 at 00:31
  • yes, flatten the list into unique elements, in the order specified by the tuples. – xiaolingxiao Dec 12 '16 at 00:32
  • @chibro2 answer should be `['excellent', 'good', 'great']` because `good` appeared before `great` in first tuple, right? – Wasi Ahmad Dec 12 '16 at 00:52
  • 2
    You've turned this into a completely different question now. Please don't do that. If you have a different question to ask, ask a new question. – TigerhawkT3 Dec 12 '16 at 00:56
  • Seriously, don't edit a question in a way that invalidates all existing answers. – TigerhawkT3 Dec 12 '16 at 00:59
  • One person read the question correctly, so at least in two people's minds, the original question is clear. here's the new question: http://stackoverflow.com/questions/41092873/given-a-linear-order-represented-as-a-list-of-tuples-of-strings-output-the-orde – xiaolingxiao Dec 12 '16 at 01:01
  • But for the other three answers (who _didn't read the question incorrectly_, I might add), and for the people who upvoted those answers, the original question wasn't merely clear, it was clearly a different question than you thought it would be. I don't have time to waste on this, so good luck. – TigerhawkT3 Dec 12 '16 at 01:17
  • @chibro2 As TigerhawkT3 has already told you, you have completely changed your question. Which means that answers given before your edit(s) may or may not work. And also, both my solution and TigerhawkT3's work for me on either of your questions. I'm, sorry, but I'm not going to keep coming back and answering each change you make to your question. Good luck, though. – Christian Dean Dec 12 '16 at 01:20

4 Answers4

2

Flatten it, remove the duplicates, and sort:

>>> i = [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
>>> sorted({x for s in i for x in s})
[1, 2, 3, 4]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
1

While @TigerhawkT3 has already given you solution, this one would work for arbitrarily nested lists/tuples:

def flatten(seq):
  for el in seq:
    if isinstance(el, (tuple, list)):
      yield from flatten(el)
    else:
      yield el

l = [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
unique = sorted({el for el in flatten(l)})
print(unique) # [1, 2, 3, 4]
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • leaf I'm sorry but this is failing this test on my machine: for l equals the example I gave, I get [excellent,good,great] – xiaolingxiao Dec 12 '16 at 00:56
0
def remove_duplicates(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x in seen or seen_add(x))]

i = [(5,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
i = remove_duplicates(list(x for s in i for x in s))
print(i)  # prints [5, 2, 1, 3, 4]

j = [('excellent','good'),('excellent','great'),('great','good')]
j = remove_duplicates(list(x for s in j for x in s))
print(j)  # prints ['excellent', 'good', 'great']

See reference: How do you remove duplicates from a list in whilst preserving order?

For explanation on the remove_duplicates() function, see this stackoverflow post.

Community
  • 1
  • 1
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
0

Isn't the following working in your case?

import itertools

lst = [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)]
final_list = sorted(set(itertools.chain(*lst)))
print(final_list)

Seems to work at least in the cases you've mentioned.

fedepad
  • 4,509
  • 1
  • 13
  • 27
  • Hey there's an implicit understanding here. In my field it is understood that one way to say a > b > c > d is to have a list of tuples of form [(a,b),(b,c),(c,d),(a,c),(a,d)...]. when I wrote my question I was thinking of a list of arbitrary items, not of integers which have an implicit ordering under the function sorted. which is why sorted works in your case, but doesn't work for arbitrary inputs. – xiaolingxiao Dec 12 '16 at 03:00
  • by the way I edited the answer a few times but some users insists on changing it back to the original, thus confusing new people. – xiaolingxiao Dec 12 '16 at 03:03