1

I have a list of lists in Python that essentially represent a table. I know I should have used a pandas dataframe to accomplish this, but hindsight is 20/20. I digress...below is an example list of what I'm talking about.

The overarching list variable is comprised of these nested lists:

['Store number', 2, 3, 4, 1, 5]
['Variable 1', 82, 99, 44, 32, 65]
['String 1', 'cat', 'dog', 'lizard', 'alligator', 'crocodile']

In the current state, each index of each nested list corresponds to the same row in the table. So for store number 2, variable 1 is 82 and string 1 is cat. Say I want to sort each list to where these relationships are maintained, but we sort by ascending store number. The final lists would look like:

['Store number', 1, 2, 3, 4, 5]
['Variable 1', 32, 82, 99, 44, 65]
['String 1', 'alligator', 'cat', 'dog', 'lizard', 'crocodile']

How could I accomplish this using my current data structures?

2 Answers 2

2

You can sort a list of indexes from a range, based on the values in one of your lists. Then you can use the indexes reorder the original three lists:

store = ['Store number', 2, 3, 4, 1, 5]
var1 = ['Variable 1', 82, 99, 44, 32, 65]
str1 = ['String 1', 'cat', 'dog', 'lizard', 'alligator', 'crocodile']

sort_indexes = sorted(range(1, len(store)), key=lambda i:store[i])

store[1:] = [store[i] for i in sort_indexes]
var1[1:] = [var1[i] for i in sort_indexes]
str1[1:] = [str1[i] for i in sort_indexes]

Your data structure would be a lot nicer to work with if the header for each column wasn't contained in the same list. But that awkwardness doesn't actually effect this code much, since we're not directly sorting the lists anyway, we're sorting indexes instead, and there's not really any problem doing that starting at index 1 instead of 0. If you move the headers out of the data, you'd just be able to skip the slice assignment at the end of this code (just use the list comprehension values directly).

Sign up to request clarification or add additional context in comments.

Comments

0

If you must use your current data structure, here is a function that will do what you want. Note there are probably much more clever approaches, but this will work.

given:

lolists = [['Store number', 2, 3, 4, 1, 5],
            ['Variable 1', 82, 99, 44, 32, 65],
            ['String 1', 'cat', 'dog', 'lizard', 'alligator', 'crocodile']      
            ]
def sortinnerLists(lol):
    kys = lol[0][1:]
    sortedKys = sorted(kys)
    rslt = [[lol[0][0]], [lol[1][0]], [lol[2][0]]]
    for v in sortedKys:
        indx = kys.index(v)+1
        rslt[0].append(v)
        rslt[1].append(lol[1][indx])
        rslt[2].append(lol[2][indx])
    return rslt    
sortinnerLists(lolists)  

yields:

[['Store number', 1, 2, 3, 4, 5],
 ['Variable 1', 32, 82, 99, 44, 65],
 ['String 1', 'alligator', 'cat', 'dog', 'lizard', 'crocodile']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.