473,385 Members | 1,856 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,385 software developers and data experts.

sorting a list complex by length of the first element

1
Hello. I need to sorting a list like z=[['111','C'],['234','A'],['12','Z'],['89','V'],['1110009','E'],['5','T']], by the length of the first element and then by his order.

Using z.sort() give [['111', 'C'], ['1110009', 'E'], ['12', 'Z'], ['234', 'A'], ['5', 'T'], ['89', 'V']]. I would to combinate the fuction 'len' but i don't know how to do it.
I try to find the soluce but I don't realy understand how use 'key' or lambda and even if it can help or if i need to make an other function...
I someone can help me... thanks
Apr 10 '14 #1
2 1329
bvdet
2,851 Expert Mod 2GB
Here's an example of using lambda to sort on the length of the first element in a list of lists.
Expand|Select|Wrap|Line Numbers
  1. >>> z=[['111','C'],['234','A'],['12','Z'],['89','V'],['1110009','E'],['5','T']]
  2. >>> sorted(z)
  3. [['111', 'C'], ['1110009', 'E'], ['12', 'Z'], ['234', 'A'], ['5', 'T'], ['89', 'V']]
  4. >>> z.sort(lambda a,b: cmp(len(a[0]), len(b[0])))
  5. >>> z
  6. [['5', 'T'], ['12', 'Z'], ['89', 'V'], ['111', 'C'], ['234', 'A'], ['1110009', 'E']]
  7. >>> z.sort(lambda a,b: cmp(len(a[0]), len(b[0])), reverse=True)
  8. >>> z
  9. [['1110009', 'E'], ['111', 'C'], ['234', 'A'], ['12', 'Z'], ['89', 'V'], ['5', 'T']]
  10. >>> 
Apr 10 '14 #2
dwblas
626 Expert 512MB
If you still don't understand how it is done, there are many sorting tutorials on the web so you can write your own sort that will
1. extract each length in turn, i.e. extract all sub-lists where the first item has a length of 1, sort it and append to the output list. Do the same for a length of 2, and continue on until there are no more elements in the original list.

2. pre-pend the length, so it gets sorted first, and the rest of the list is then sorted in order as in the code below
Expand|Select|Wrap|Line Numbers
  1. z=[['234','A'],['111','C'],['12','Z'],['12','V'],
  2.    ['1110009','E'],['5','T'],['20','B']]
  3. z2 = [[len(x[0])] + x for x in z]
  4.  
  5. print z2, "\n"
  6. z2.sort()
  7. print z2 
Apr 10 '14 #3

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

Similar topics

8
by: ehames | last post by:
Hi guys, As far as I know, for any type T, and positive integer N, if I have an array declared as: T array; then, &array and array are the same element. Is there any reason why a
2
by: Ben Bush | last post by:
I have a lis: ,,,,,] I want a code to test when the difference between the first element in the list of list is equal to or larger than 6, then move the previous lists to the end of the list....
10
by: garyusenet | last post by:
Hi I'm using the following to reference each 'element' (is this the correct term) in an arraylist: - foreach (InternetExplorer ie in ar) { ... } Q. How do I reference the nth element in an...
3
by: Arkady Frenkel | last post by:
Hi, guys! Is it possible to write byte array, I receive already from one stream to another, but not from first element, without copy part of array to new one. I mean I have byte array and need to...
1
by: Giovanni Toffoli | last post by:
Hi, I'm not in the mailing list. By Googling, I stepped into this an old post: (Thu Feb 14 20:40:08 CET 2002) of Jeff Shannon:...
3
by: jm.suresh | last post by:
Hi, I have a list and I want to find the first element that meets a condition. I do not want to use 'filter', because I want to come out of the iteration as soon as the first element is found. I...
10
by: Chris Forone | last post by:
Hello Group, there is some memberfunc for std::valarray to return a pointer to the first element in the array. How do i use this? Thanx a lot. HAND Chris
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
2
by: DD2783 | last post by:
I'm currently trying to remove the first element in an int array using objects in C#. I have just coded how to add an element to that start of the array and though it was just a case of changing a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
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,...

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.