Combining a 2D list of tuples and sorting them in Python

Update: the list is filled with lines. I edited the list to show this.

I have 3 different lists e.g.

Section = [('1', '1.1', '1.2'), ('1', '2', '2.2', '3'), ('1', '1.2', '3.2', '3.5')] Page = [('1', '1', '3'), ('1', '2', '2', '2'), ('1', '2', '3', '5')] Titles = [('General', 'Info', 'Titles'), ('More', 'Info', 'Section', 'Here'), ('Another', 'List', 'Of', 'Strings')] 

I want to combine them, for example

 Combined_List = [('1', '1.1', '1.2'), ('1', '2', '2.2', '3'), ('1', '1.2', '3.2', '3.5'), ('1', '1', '3'), ('1', '2', '2', '2'), ('1', '2', '3', '5'), ('General', 'Info', 'Titles'), ('More', 'Info', 'Section', 'Here'), ('Another', 'List', 'Of', 'Strings')] 

Or any other form that allows me to sort them by numbers in the named sections.

In this case, it will be

  Sorted_list = [('1', '1', '1', '1.1', '1.2', '1.2', '2', '2.2', '3', '3.2', '3.5'), ('1', '1', '1', '1', '3', '2', '2', '2', '2', '3', '5'), ('General', 'More', 'Another', 'Info', 'Titles', 'List', 'Info', 'Section', 'Here', 'Of', 'Strings') 

I need this so that I can eventually export the sorted list by section in excel. If you can think of a more efficient way to display / format, please share!

+7
python sorting list
source share
3 answers

Try it,

 Section = [('1', '1.1', '1.2'), ('1', '2', '2.2', '3'), ('1', '1.2', '3.2', '3.5')] Page = [('1', '1', '3'), ('1', '2', '2', '2'), ('1', '2', '3', '5')] Titles = [('General', 'Info', 'Titles'), ('More', 'Info', 'Section', 'Here'), ('Another', 'List', 'Of', 'Strings')] # Flat a list of tuples into a list l1 = [item for sublist in Section for item in sublist] l2 = [item for sublist in Page for item in sublist] l3 = [item for sublist in Titles for item in sublist] # Python2, `zip` returns a list of tuples #result = zip(*sorted(zip(l1, l2, l3), key=lambda x:float(x[0]))) # Python3, `zip` returns an iterator of tuples result = list(zip(*sorted(zip(l1, l2, l3), key=lambda x:float(x[0])))) print(result) # Output [ ('1', '1', '1', '1.1', '1.2', '1.2', '2', '2.2', '3', '3.2', '3.5'), ('1', '1', '1', '1', '3', '2', '2', '2', '2', '3', '5'), ('General', 'More', 'Another', 'Info', 'Titles', 'List', 'Info', 'Section', 'Here', 'Of', 'Strings')] 
+2
source share

You can do:

 from itertools import chain tuples = zip(map(float, list(chain(*Section))), list(chain(*Page)), list(chain(*Title))) zip(*sorted(tuples, key=lambda x: x[0])) Out[232]: [(1.0, 1.0, 1.0, 1.1, 1.2, 1.2, 2.0, 2.2, 3.0, 3.2, 3.5), ('1', '1', '1', '1', '3', '2', '2', '2', '2', '3', '5'), ('General', 'More', 'Another', 'Info', 'Titles', 'List', 'Info', 'Section', 'Here', 'Of', 'Strings')] 

Here you first disable your three lists (which is what list(chain(*L)) does list(chain(*L)) ) and pack them in tuples using zip . Tip "tuples" to see what it looks like.

Then, on the second line of code, you can apply sorting according to the element of the required tuple. And you unpack the result.

+2
source share

My approach. List accounting and no need to import modules. I think it is fast and very simple.

EDIT: I added an unsorted approach and a sorted approach.

 #Unsorted newList =[ [item for sublist in Section for item in sublist], [item for sublist in Page for item in sublist], [item for sublist in Titles for item in sublist] ] print newList #Output #[[1, 1.1, 1.2, 1, 2, 2.2, 3, 1, 1.2, 3.2, 3.5], # [1, 1, 3, 1, 2, 2, 2, 1, 2, 3, 5], # ['General', 'Info', 'Titles', 'More', 'Info', 'Section', 'Here', 'Another', 'List', 'Of', 'Strings']] #Sort first two lists afterwards, if desired for i in range(2): newList[i].sort() print newList #Output #[[1, 1, 1, 1.1, 1.2, 1.2, 2, 2.2, 3, 3.2, 3.5], # [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 5], # ['General', 'Info', 'Titles', 'More', 'Info', 'Section', 'Here', 'Another', 'List', 'Of', 'Strings']] 
-one
source share

All Articles