Ok,so I have 2 lists with the same lenght.The one has etc. names of students and the other has how many courses a student has passed and these lists ara parallel.I want to sort out the second list so I can find the most courses passed by a student,but how can the first list be sorted in the same way so the right student is shown?Can I use the "sort" method in some way or I have to do a whole different proccess?
Thank you in advance.
Following are a couple of ways: - >>> names = ["Bill", "Jane", "Harrold", "Nancy"]
-
>>> courses_passed = [12, 6, 14, 19]
-
>>> names = ["Bill", "Jane", "Harold", "Nancy"]
-
>>> courses_passed = [12, 6, 14, 19]
-
>>> combined = zip(names, courses_passed)
-
>>> combined
-
[('Bill', 12), ('Jane', 6), ('Harold', 14), ('Nancy', 19)]
-
>>> combined.sort(lambda x,y: cmp(x[1], -y[1]))
-
>>> combined
-
[('Nancy', 19), ('Harold', 14), ('Bill', 12), ('Jane', 6)]
-
>>> print "%s passed %s courses" % (combined[0])
-
Nancy passed 19 courses
-
>>> most = max(courses_passed)
-
>>> names[courses_passed.index(most)]
-
'Nancy'
-
>>>
2 6866 bvdet 2,851
Expert Mod 2GB
Following are a couple of ways: - >>> names = ["Bill", "Jane", "Harrold", "Nancy"]
-
>>> courses_passed = [12, 6, 14, 19]
-
>>> names = ["Bill", "Jane", "Harold", "Nancy"]
-
>>> courses_passed = [12, 6, 14, 19]
-
>>> combined = zip(names, courses_passed)
-
>>> combined
-
[('Bill', 12), ('Jane', 6), ('Harold', 14), ('Nancy', 19)]
-
>>> combined.sort(lambda x,y: cmp(x[1], -y[1]))
-
>>> combined
-
[('Nancy', 19), ('Harold', 14), ('Bill', 12), ('Jane', 6)]
-
>>> print "%s passed %s courses" % (combined[0])
-
Nancy passed 19 courses
-
>>> most = max(courses_passed)
-
>>> names[courses_passed.index(most)]
-
'Nancy'
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ryan Lowe |
last post by:
maybe its just me, but the behavior of parallel lists in for loops seems
backwards. why doesnt it mirror parallel assignment? i think tuple-unpacking
should take precedence, but instead iteration...
|
by: Kamilche |
last post by:
I've written a generic sort routine that will sort dictionaries,
lists, or tuples, either by a specified key or by value.
Comments welcome!
import types
def sort(container, key = None,...
|
by: Hagen |
last post by:
Hello,
got a short question about the sort algorithm of the list container in the
Standard Template Library:
Can I use the sort algorithm with another parameter to sort by? Sort uses
the...
|
by: Xiaozhu |
last post by:
say, if you had parallel arrays containing the sales person id, the month, and
the sales figures (for that person in that month), sorting on sales figure and
preserve the order of figures within...
|
by: Sean Berry |
last post by:
Hello all
I have build a list that contains data in the form below
-- simplified for question --
myList = ,, ...]
I have a function which takes value3 from the lists above and returns...
|
by: Majnu |
last post by:
Hi community,
just in case somebody needs a shellsort in c#, I rewrote the pascal
code that I found in another newsgroup. Here are both. For more
explanation on the pascal code you can search...
|
by: uppe |
last post by:
Hey everyone,
I've just finished my implementation of the merge-sort algorithm in C,
and I thought I could ask for some feedback. (One can always improve,
they say)
Right now, the code sorts...
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort...
|
by: Tem |
last post by:
List<inta = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
List<intb = new List<int>();
b.Add(3);
b.Add(4);
b.Add(5);
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |