Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Parallel Sorting

Tags:

python

sorting

I have list1:

['GM2', 'GM1', 'GM3']

and list2:

['A', 'B', 'C']

How do you sort this in a way where it shows. Basically would like the element at index 0 in list1 to correspond to the element in index 0 in list2.

GM1 B
GM2 A
GM3 C
like image 577
Aurora_Titanium Avatar asked Jul 18 '26 07:07

Aurora_Titanium


1 Answers

The builtin zip function will match the corresponding elements so that you get a result of tuples, where each element is made up from an element in list1 and its corresponding element in list2:

>>> list1 = ['GM2', 'GM1' ,'GM3']
>>> list2 = ['A', 'B', 'C']
>>> result = sorted(zip(list1, list2))
>>> result
[('GM1', 'B'), ('GM2', 'A'), ('GM3', 'C')]
like image 126
Mureinik Avatar answered Jul 19 '26 21:07

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!