Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked Related Hot Network Questions. Viewed 14k times. Improve this question. Roman Roman 1 1 gold badge 1 1 silver badge 4 4 bronze badges.
Add a comment. Active Oldest Votes. Improve this answer. Community Bot 1. Grajdeanu Alex Grajdeanu Alex 8, 3 3 gold badges 27 27 silver badges 63 63 bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
It sorts list2 according to corresponding values in list1, but make sure that while using this, no two values in list2 evaluate to be equal because list. Another approach to retaining the order of a string list when sorting against another list is as follows:.
I would like to expand open jfs's answer , which worked great for my problem: sorting two lists by a third, decorated list :. We can create our decorated list in any way, but in this case we will create it from the elements of one of the two original lists, that we want to sort:.
Now we can apply jfs's solution to sort our two lists by the third. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. How to sort two lists which reference each other in the exact same way Ask Question. Asked 9 years, 10 months ago. Active 14 days ago. Viewed k times. Improve this question. Georgy 7, 7 7 gold badges 54 54 silver badges 64 64 bronze badges. Lostsoul Lostsoul I should point out that your variables in list2 don't point to the ints in list1. Add a comment. Active Oldest Votes. Improve this answer. But in another sense, they're hardly different at all.
It's effectively a transposition operator. Show 3 more comments. The cool thing about this is that I can keep indexes around and sort other stuff later, in case list1 is an important coordinate that affects several other arrays. DonQuiKong you also need to list around map if you'd like to use this code in Python 3. Daniel Thaagaard Andreasen Daniel Thaagaard Andreasen 6, 5 5 gold badges 27 27 silver badges 39 39 bronze badges.
I get an error running your example: TypeError: only integer arrays with one element can be converted to an index Python 2. To fix it, list1 and list2 must be declared as numpy arrays.
Isn't this what I write in the comment in the function? Anyway, I think it's silly that np. I was referring to the first code snippet since it doesn't run as written : — BenB. I corrected it by converting the lists when they are assigned to numpy arrays.
0コメント