473,326 Members | 2,136 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,326 software developers and data experts.

Is there any way that i can sort a List like this?

Expand|Select|Wrap|Line Numbers
  1. List =[("Rail",2),("Airport",6),("Interstate",1),("jack",5),("Port",3),("intermodal",4),("MinPopulation",8),("Etc",7)]
i want to sort it like:
Expand|Select|Wrap|Line Numbers
  1.                   "Interstate",1
  2.                         "Rail",2
  3.                         "Port",3
  4.                         "intermodal",4
  5.                         "jack",5
  6.                         "Airport",6
  7.                         "Etc",7
  8.                         "MinPopulation",8
I TRIED USING SORTED()BUT WAS NOT ABLE TO OBTAIN DESIRED RESULT:

Expand|Select|Wrap|Line Numbers
  1.         sorted(List,key=operator.itemgetter(1))
cAN SOMEONE HELP ME WITH A BETTER SOLUTION?
Feb 8 '11 #1
1 1478
bvdet
2,851 Expert Mod 2GB
This is a solution for Python 2.3:
Expand|Select|Wrap|Line Numbers
  1. >>> List =[("Rail",2),("Airport",6),("Interstate",1),("jack",5),("Port",3),("intermodal",4),("MinPopulation",8),("Etc",7)]
  2. >>> List.sort(lambda a,b: cmp(a[1], b[1]))
  3. >>> List
  4. [('Interstate', 1), ('Rail', 2), ('Port', 3), ('intermodal', 4), ('jack', 5), ('Airport', 6), ('Etc', 7), ('MinPopulation', 8)]
  5. >>> 
Feb 8 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jean-Paul Lauque | last post by:
Hello, I'm beginning in the ASP world... I would like to sort (descending) list of files in directory. Parameter is directory url. How I can to do that with ASP not ASP.NET.
4
by: JC | last post by:
Hello, I'm running into a huge wall with linked lists. If anyone outhere can help me, I'll appreciate it very much. Here is my delima! I need to create a simple list of numbers. which I...
3
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
0
by: Daniel | last post by:
how do i call a stored proc that looks like this from ADO.NET? ALTER PROCEDURE new_pzGetIDList ( @foo varbinary(8) ) i have it as a System.Byte in C#
7
by: Lad | last post by:
I have a list of emails and I would like to sorted that list by domains E.g. If the list is Emails= after sorting I would like to have Emails= What is the best/easiest way?
31
by: Lane Straatman | last post by:
void s_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { size_t bytes; unsigned char *array, *after, *i, *j, *k, *p1, *p2, *end, swap; array = base;...
6
by: Tem | last post by:
Thanks for all your responses. I see why c# is such a powerful language and starting to like it more and more. I also need help sorting this nested list. It is a little tricky. not sure where to...
5
by: jr | last post by:
I created a simple class, have used it to populate a list (of myclass) - The class only contains a customer id and a date. I would like to sort this list based on most recent date to the oldest....
3
by: Tem | last post by:
What's a good way to sort a List<stringby the length of the strings from longest to shortest. in c# 3 "aaaaaa" "aaaaaaaaaa" "aaa" becomes "aaaaaaaaaa" "aaaaaa"
3
by: fastestindian | last post by:
Hi, I am working on the project where i show a list of Log Records. my list is as follows. Original list Id Time Event Details 1 1 Error1 xyz 2 1 ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
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...
0
isladogs
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 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.