Home > Net >  How to generate ordered lists from a complex list (string, integer, tuple) in python?
How to generate ordered lists from a complex list (string, integer, tuple) in python?

Time:10-09

I have a list in python3 that is composed by several sub-lists, each one with a string, a counting integer, and a tuple:

final =[ ['auto', 3, [1066, 1238, 1700]],
         ['conta', 3, [1286, 1495, 1550]],
         ['multidisciplinar', 3, [1301, 1428, 1451]],
         ['intradisciplinar', 3, [1302, 1508, 1559]],
         ['variações', 3, [1314, 1426, 1937]],
         ['alegoria', 3, [1339, 1376, 1630]],
         ['orquestra', 3, [1351, 1370, 1394]],
         ['exemplo', 3, [1372, 1741, 2092]],
         ['natureza', 3, [1480, 1533, 1737]],
         ['procedimentos', 3, [1552, 1782, 2008]],
         ['tema', 3, [1577, 1647, 1696]],
         ['acadêmico', 2, [3, 397]],
         ['adquirido', 2, [15, 850]],
         ['científico', 2, [22, 1764]],
         ['área', 2, [32, 1400]],
         ['facilmente', 2, [44, 496]],
         ['antigüidade', 2, [1837, 1885]],
         ['através', 2, [1853, 1880]],
         ['primeiros', 2, [1856, 1891]],
         ['complexidade', 2, [1869, 1943]],
         ['partir', 2, [1915, 2051]],
         ['elemento', 2, [1917, 1938]],
         ['água', 2, [1925, 1966]],
         ['ar', 2, [1926, 1965]],
         ['fogo', 2, [1928, 1967]],
         ['coisa', 2, [2029, 2038]],
         ['poderiam', 2, [2048, 2086]],
         ['diferenças', 2, [2059, 2078]],
         ['entrou', 1, [1]],
         ['vocabulário', 1, [2]],
         ['usual', 1, [4]],
         ['timida', 1, [5]],
         ['mente', 1, [6]],
         ['tateando', 1, [7]] ]

As you all can see, this list orders the strings (first element), counting their frequencies from higher to lower (second element), and also count the position where the string element appears (third element, the tuple). Based on that, I need to generate another four different lists containing these sub-lists, i.e with the same structure, but with the elements ordered like this:

A first list where the strings (first element) are decreasingly ordered considering their frequencies (second element) and the lowest value for the first element in the tuple (third element). This denotes a higher frequency and lower order of the string. Already provided.

A second list where the strings (first element) are decreasingly ordered considering their frequencies (second element) and the highest value for the first element in the tuple (third element). This denotes a higher frequency and higher order of the string.

A third list where the strings (first element) are increasingly ordered considering their frequencies (second element) and the lowest value for the first element in the tuple (third element). This denotes a lower frequency and lower order of the string.

A fourth list where the strings (first element) are increasingly ordered considering their frequencies (second element) and the highest value for the first element in the tuple (third element). This denotes a lower frequency and higher order of the string.

First list - higher frequency and lower order.

Second list - higher frequency and higher order.

Third list - lower frequency and lower order.

Fourth list - lower frequency and higher order.

All the considerations about the order of elements in the tuples apply only to those tuples that have more than one element.

I sense with the force that it can be done, but I've tried all approaches using lists, indexes, pandas and dictionaries that I can understand, and I still cannot figure how to do it. Thanks in advance.

CodePudding user response:

You can sort your list using lambda functions for each items attribute upon which want to sort. Using the reverse flag will sort from highest to lowest. So, to build your fourth list;

sorted(sorted(final, key=lambda x: x[2][0], reverse=True), key = lambda x: x[1])

Edit:

If you don't a slight hack, then you can use one sorted call - in this case the sorting is all from smallest to largest, but to reverse it you can make the search criteria the negated value. So again, the fourth list would be;

sorted(final, key=lambda x: (x[1], -x[2][0]))

CodePudding user response:

The variations of the code that worked to generate the three missing lists were:

# Lower freq - High order
  alt_1 = sorted(sorted(final, key=lambda x: x[2][0], reverse=True), key = lambda x: x[1])

# Higher freq - High order
  alt_2 = sorted(sorted(final, key=lambda x: x[2][0], reverse=True), key = lambda x: x[1], reverse=True)

# Lower freq - Lower order
  alt_3 = sorted(sorted(final, key=lambda x: x[2][0]), key = lambda x: x[1])
  • Related