Python: Sort lists equally?

Hello, suppose I have three lists:

A=['1_1','1_6','1_3']

each element in A is simply a string snippet (which will later point to a .jpg)

B=['x','b','s']

and a list C=['p','i','j']

I want to sort A in the order 1_1,1_3,1_6.

In the same way that A was sorted, B and C should then be sorted. The lists will be processed later. How do you implement this?

VG

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
12 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
mihisu
2 years ago

You could put the lists together, then sort them and then drag them back into separate lists. [This is like a “decorate-sort-undecorate”.]

For example, so…

A = ['1_1', '1_6', '1_3']
B = ['x', 'b', 's']
C = ['p', 'i', 'j']

A, B, C = zip(*sorted(zip(A, B, C)))

And then…

A = ('1_1', '1_3', '1_6')
B = ('x', 's', 'b')
C = ('p', 'j', 'i')

If you want to have lists again instead of tuple… Then add the list function…

A = ['1_1', '1_6', '1_3']
B = ['x', 'b', 's']
C = ['p', 'i', 'j']

A, B, C = map(list, zip(*sorted(zip(A, B, C))))
mihisu
2 years ago

Well. That’s what’s sorted by the elements of list A right now.

If there are different things in the strings contained in A, then this is sorted first by the front part due to the alphabetical sorting.

You could, for example, specify a key-function at the sorted() that will get you the corresponding part to be sorted by.

For example, if you have an underscore in there and only have to be sorted by the underscore…

A = ['1_1', '1_6', '1_3', '2_4']
B = ['x', 'b', 's', 't']
C = ['p', 'i', 'j', 'k']

A, B, C = map(list, zip(*sorted(zip(A, B, C), key=lambda L: L[0].split('_')[-1])))

print(A)  # ['1_1', '1_3', '2_4', '1_6']
print(B)  # ['x', 's', 't', 'b']
print(C)  # ['p', 'j', 'k', 'i']
mihisu
2 years ago

I also have to say I understand the part with the key lambda l etc. also not

The sorted() function can be passed a key function as a parameter. The elements of the list to be sorted are put into the key function, and according to the values that come out as key values, the list is then sorted.

In the specific case (in the original example), the first value L[0] is taken for each element L of zip(A, B, C). These are the strings ‘1_1’, ‘1_3’, ‘2_4’, ‘1_6’ in the example. With the split() method, these strings are split onto each ‘_’ at L[0].split(‘_’). From ‘1_3’, for example (‘1’, ‘3’). With the index [-1] behind it, the last part string is searched out, so for ‘1_3’ the string ‘3’ behind the last ‘_’, for example. And then it’s sorted.

mihisu
2 years ago

So if the values behind the ‘-‘ are decisive for you, of course, you must split ‘-‘ instead of ‘_’ in the split I defined in the key-function. If you change that, it should fit.

Example:

A = ['2301239k-12', '2301239a-13', '2301239k-14', '2301239k-25', '2301239d-27', '2301239k-28', '2301239a-02', '2301239a-03', '2301239b-03']
B = ['b01', 'b02', 'b03', 'b04', 'b05', 'b06', 'b07', 'b08', 'b09']
C = ['c01', 'c02', 'c03', 'c04', 'c05', 'c06', 'c07', 'c08', 'c09']

print("Davor:")
print(f"A = {A}")
print(f"B = {B}")
print(f"C = {C}")

A, B, C = map(list, zip(*sorted(zip(A, B, C), key=lambda L: L[0].split('-')[-1])))

print("Danach:")
print(f"A = {A}")
print(f"B = {B}")
print(f"C = {C}")

And, yes. In this example, an alphabetical order would be sorted. Thus, for example, a ‘…-14’ would be sorted in front of a ‘…-2’. This could be further refined. For example, if you expect only numbers behind the ‘-‘, you could simply cast the back part with int() into a whole number and then sort it according to the numbers. Then, a 2 is, for example, in front of a 14 .

Example:

A = ['2301239k-12', '2301239a-13', '2301239k-14', '2301239k-25', '2301239d-27', '2301239k-28', '2301239a-2', '2301239a-3', '2301239b-3']
B = ['b01', 'b02', 'b03', 'b04', 'b05', 'b06', 'b07', 'b08', 'b09']
C = ['c01', 'c02', 'c03', 'c04', 'c05', 'c06', 'c07', 'c08', 'c09']

print("Davor:")
print(f"A = {A}")
print(f"B = {B}")
print(f"C = {C}")

A, B, C = map(list, zip(*sorted(zip(A, B, C), key=lambda L: int(L[0].split('-')[-1]))))

print("Danach:")
print(f"A = {A}")
print(f"B = {B}")
print(f"C = {C}")
mihisu
2 years ago

Well, but that’s what I’m saying, isn’t it?

mihisu
2 years ago

Sorry, but I don’t understand.

What was “knipst” there? Where’s that going?