How to convert a pair of values ​​into a sorted unique array?

I have the result as follows:

[(196, 128), (196, 128), (196, 128), (128, 196), (196, 128), (128, 196), (128, 196), (196, 128), (128, 196), (128, 196)] 

And I would like to convert it to unique values ​​like this in sorted order:

 [128, 196] 

And I'm pretty sure that there is something like a one-line trick in Python (including batteries), but I can't find it.

+5
source share
1 answer

Create a union of the connections of all the tuples, then sort the result:

 sorted(set().union(*input_list)) 

Demo:

 >>> input_list = [(196, 128), (196, 128), (196, 128), (128, 196), ... (196, 128), (128, 196), (128, 196), (196, 128), ... (128, 196), (128, 196)] >>> sorted(set().union(*input_list)) [128, 196] 
+9
source

Source: https://habr.com/ru/post/1244001/


All Articles