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
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…
And then…
If you want to have lists again instead of tuple… Then add the list function…
Thank you, I’ll try this out. If, however, no string would be in front of the respective element, would it be included in me in alphabetical order?
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…
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.
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:
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:
Example of sorting I get; for me the values are crucial after the ‘-‘ , so it should be order 1, 2, 3, 10, 11, 12 etc. and not 10,11,12,1,2,3 etc. I think it’s just a little change that’s what’s going on, but I can’t find the place. It was already thought that it was not 01, 02, 03, 10,11,12 or so, but that also solved net (without the 0 before numbers “length 1” the sorting is the same s.u.). Somehow he doesn’t seem to know that the 10 is bigger than the 1 o0
2301239k-12 2301239k-12
2301239a-13 2301239a-13
2301239k-14 2301239k-14
2301239k-25 2301239k-25
2301239d-27 2301239d-27
2301239k-28 2301239k-28
2301239a-02 2301239a-02
2301239a-03 2301239a-03
2301239b-03 2301239b-03
So what he does is to pack the 13 before the 2 unfortunately:(but I also have to say I understand the part with the key lambda l etc. also not:(
Well, but that’s what I’m saying, isn’t it?
Suppose the A = [‘AString1_1’, ‘BString1_6’, ‘FString1_3’, ‘DString2_4’]
are now sorted to [‘1_1’, ‘1_3’, ‘2_4’, ‘1_6’], but now each element should stand as before (just assorted), so [‘AString1_1’, ‘FString1_3’, ‘DString2_4’, ‘BString1_6’]
Sorry, but I don’t understand.
What was “knipst” there? Where’s that going?
Hm thank you., so the problem is, it should not only be sorted, so according to which it was cut off, it should be finished again where it was before. Is that okay?
Thank you, I’m gonna test this:)