473,388 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Sorting x lists based on one list

Hi,

I'm looking for an easy algorithm - maybe Python can help:

I start with X lists which intial sort is based on list #1.

I want to reverse sort list #1 and have all other lists sorted accordingly.

Any idea is welcome.

Regards,

Philippe

Jul 19 '05 #1
10 2147
l1 = ['a','b','c']
l2 = ['toto','titi','tata'] # 'toto' refers to 'a', 'titi' to b' ....
l3 = ['foo','bar','doe'] # 'foo' refers to 'a' ........

I want to reverse sort l1 and have l2 and l3 follow accordingly.

Regards,

Philippe


Philippe C. Martin wrote:
Hi,

I'm looking for an easy algorithm - maybe Python can help:

I start with X lists which intial sort is based on list #1.

I want to reverse sort list #1 and have all other lists sorted
accordingly.

Any idea is welcome.

Regards,

Philippe


Jul 19 '05 #2
> Philippe C. Martin wrote:
I'm looking for an easy algorithm - maybe Python can help:
I start with X lists which intial sort is based on list #1.
I want to reverse sort list #1 and have all other lists sorted
accordingly.
One way, using a helper list with indices:
l1 = ['a','b','c']
l2 = ['toto','titi','tata'] # 'toto' refers to 'a', 'titi' to b' ....
l3 = ['foo','bar','doe'] # 'foo' refers to 'a' ........
indices = sorted(range(len(l1)), key=l1.__getitem__, reverse=True)
for items in l1, l2, l3: .... items[:] = [items[i] for i in indices]
.... l1 ['c', 'b', 'a'] l2 ['tata', 'titi', 'toto'] l3

['doe', 'bar', 'foo']

Another way would be to merge the three lists into one of 3-tuples, sort,
and unmerge, similarly to the DSU pattern -- which raises the question: why
are you using three lists in the first place?

Peter
Jul 19 '05 #3
> Another way would be to merge the three lists into one of 3-tuples, sort,
and unmerge, similarly to the DSU pattern -- which raises the question:
why are you using three lists in the first place?


:-) Thanks, the lists will evolve and are also stored in 'csv' format in
external files at one point. I cannot use dictionaries because I need to
control the sorting (hash).

In this specific case, list 1 represents students with their information,
list 2 represents assignments with information such as weight, term, max
grade ... and list 3 the actual grades.

Regards,

Philippe

Peter Otten wrote:
Philippe C. Martin wrote:
I'm looking for an easy algorithm - maybe Python can help:
I start with X lists which intial sort is based on list #1.
I want to reverse sort list #1 and have all other lists sorted
accordingly.
One way, using a helper list with indices:
l1 = ['a','b','c']
l2 = ['toto','titi','tata'] # 'toto' refers to 'a', 'titi' to b' ....
l3 = ['foo','bar','doe'] # 'foo' refers to 'a' ........
indices = sorted(range(len(l1)), key=l1.__getitem__, reverse=True)
for items in l1, l2, l3: ... items[:] = [items[i] for i in indices]
... l1 ['c', 'b', 'a'] l2 ['tata', 'titi', 'toto'] l3

['doe', 'bar', 'foo']

Another way would be to merge the three lists into one of 3-tuples, sort,
and unmerge, similarly to the DSU pattern -- which raises the question:
why are you using three lists in the first place?

Peter


Jul 19 '05 #4
I will look at that merge/unmerge thing
Peter Otten wrote:
Philippe C. Martin wrote:
I'm looking for an easy algorithm - maybe Python can help:
I start with X lists which intial sort is based on list #1.
I want to reverse sort list #1 and have all other lists sorted
accordingly.
One way, using a helper list with indices:
l1 = ['a','b','c']
l2 = ['toto','titi','tata'] # 'toto' refers to 'a', 'titi' to b' ....
l3 = ['foo','bar','doe'] # 'foo' refers to 'a' ........
indices = sorted(range(len(l1)), key=l1.__getitem__, reverse=True)
for items in l1, l2, l3: ... items[:] = [items[i] for i in indices]
... l1 ['c', 'b', 'a'] l2 ['tata', 'titi', 'toto'] l3

['doe', 'bar', 'foo']

Another way would be to merge the three lists into one of 3-tuples, sort,
and unmerge, similarly to the DSU pattern -- which raises the question:
why are you using three lists in the first place?

Peter


Jul 19 '05 #5
Why not merge the lists together using zip() and then
sort.

info=zip(l1, l2, l3)
info.sort()
info.reverse

Larry Bates

Philippe C. Martin wrote:
l1 = ['a','b','c']
l2 = ['toto','titi','tata'] # 'toto' refers to 'a', 'titi' to b' ....
l3 = ['foo','bar','doe'] # 'foo' refers to 'a' ........

I want to reverse sort l1 and have l2 and l3 follow accordingly.

Regards,

Philippe


Philippe C. Martin wrote:

Hi,

I'm looking for an easy algorithm - maybe Python can help:

I start with X lists which intial sort is based on list #1.

I want to reverse sort list #1 and have all other lists sorted
accordingly.

Any idea is welcome.

Regards,

Philippe


Jul 19 '05 #6
I had no clue this was feasible!

Python folks should get the Nobel price !


Larry Bates wrote:
Why not merge the lists together using zip() and then
sort.

info=zip(l1, l2, l3)
info.sort()
info.reverse

Larry Bates

Philippe C. Martin wrote:
l1 = ['a','b','c']
l2 = ['toto','titi','tata'] # 'toto' refers to 'a', 'titi' to b' ....
l3 = ['foo','bar','doe'] # 'foo' refers to 'a' ........

I want to reverse sort l1 and have l2 and l3 follow accordingly.

Regards,

Philippe


Philippe C. Martin wrote:

Hi,

I'm looking for an easy algorithm - maybe Python can help:

I start with X lists which intial sort is based on list #1.

I want to reverse sort list #1 and have all other lists sorted
accordingly.

Any idea is welcome.

Regards,

Philippe



Jul 19 '05 #7
Philippe C. Martin wrote:
:-) Thanks, the lists will evolve and are also stored in 'csv' format in
external files at one point. I cannot use dictionaries because I need to
control the sorting (hash).

In this specific case, list 1 represents students with their information,
list 2 represents assignments with information such as weight, term, max
grade ... and list 3 the actual grades.


This sounds like you should seriously consider using a database.

Peter
Jul 19 '05 #8
Philippe C. Martin wrote:
Another way would be to merge the three lists into one of 3-tuples, sort,
and unmerge, similarly to the DSU pattern -- which raises the question:
why are you using three lists in the first place?

:-) Thanks, the lists will evolve and are also stored in 'csv' format in
external files at one point. I cannot use dictionaries because I need to
control the sorting (hash).

In this specific case, list 1 represents students with their information,
list 2 represents assignments with information such as weight, term, max
grade ... and list 3 the actual grades.

Regards,

Philippe


Hi Philippe,

As Peter suggested this sounds like a database. Especially if it will
evolve and grow to the point where it will not all fit in memory. Also
data bases already have a lot of ability to query, and generate sorted
reports built into them.

This is a fairly normal task for a relational data base where you have a
transaction list, (grades), that are connected to other lists,
(students, and assignments). This saves a lot of space by not
duplicating the student information and assignment information for each
grade given. It also makes updating student information easier because
it only needs to be updated once, and not changed everwhere it's referenced.

If your lists are not going to grow larger than what will fit in memory,
you can use dictionaries and lists. Loosely like the following...

- Read student info from cvs file into dictionary using student_id or
number as a key. Names can be used if they are unique. The content of
each student file will be a list, with None for missing items.

students[student_id] = (name, address, phone, etc....)

- Read assignments into dictionary using an assignment_no as key.

assignments[assignment_no] = (a_name, description, etc...)

- Create a grades list which will contain grade tuples:

[(date,student_id,assignment_no,grade), ... more grades ... ]
You can sort your grades list however you want. If you want to sort by
student name instead of student_id, you would use:

# Sort grades list by student name.
grades.sort(lambda x,y: cmp(students[x[1]][0], students[y[1]][0]))

Assuming the name is in the first field in the student dictionary-value
tuple. There are probably other ways to do this that are more readable
or faster.

The grade list could also be filtered, so if you want to get all the
grades for a specific student, or all the grades for a particular
assignment you can just loop through grades list and print what you want.

An alternative query would be to get all the students who have not
completed an assignment: Get a list of all the student who have a grade
for the assignment, then use that to get a list from the student
dictionary, who are not in the students_completed_assignment list.

Anyway, just trying to give you some ideas.

Cheers,
_Ron
Jul 19 '05 #9
Ron Adam wrote:
You can sort your grades list however you want. If you want to sort by
student name instead of student_id, you would use:

# Sort grades list by student name.
grades.sort(lambda x,y: cmp(students[x[1]][0], students[y[1]][0]))

Assuming the name is in the first field in the student dictionary-value
tuple. There are probably other ways to do this that are more readable
or faster.


Assuming that students[x[1]][0] is what you want to sort on, this may
also be written as:

grades.sort(key=lambda x: students[x[1]][0])

It will probably be somewhat faster, but depending on the size of your
data, you may not notice.

STeVe
Jul 19 '05 #10
Steven Bethard wrote:
Ron Adam wrote:
grades.sort(lambda x,y: cmp(students[x[1]][0], students[y[1]][0]))
Assuming that students[x[1]][0] is what you want to sort on, this may
also be written as:

grades.sort(key=lambda x: students[x[1]][0])

Yes, I figured there was a better way to write it.

Thanks, Steve

Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: Susan.Adkins | last post by:
Alright, I must say that the problem my teacher has given us to do has royally annoyed me. We are to take a letter *txt file* and then read in the words. Make 2 seperate lists *even and odd* for...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
7
by: apotheos | last post by:
I can't seem to get this nailed down and I thought I'd toss it out there as, by gosh, its got to be something simple I'm missing. I have two different database tables of events that use different...
4
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.