473,403 Members | 2,293 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,403 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 1330
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.