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
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. - >>> z=[['111','C'],['234','A'],['12','Z'],['89','V'],['1110009','E'],['5','T']]
-
>>> sorted(z)
-
[['111', 'C'], ['1110009', 'E'], ['12', 'Z'], ['234', 'A'], ['5', 'T'], ['89', 'V']]
-
>>> z.sort(lambda a,b: cmp(len(a[0]), len(b[0])))
-
>>> z
-
[['5', 'T'], ['12', 'Z'], ['89', 'V'], ['111', 'C'], ['234', 'A'], ['1110009', 'E']]
-
>>> z.sort(lambda a,b: cmp(len(a[0]), len(b[0])), reverse=True)
-
>>> z
-
[['1110009', 'E'], ['111', 'C'], ['234', 'A'], ['12', 'Z'], ['89', 'V'], ['5', 'T']]
-
>>>
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 - z=[['234','A'],['111','C'],['12','Z'],['12','V'],
-
['1110009','E'],['5','T'],['20','B']]
-
z2 = [[len(x[0])] + x for x in z]
-
-
print z2, "\n"
-
z2.sort()
-
print z2
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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....
|
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...
|
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...
|
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:...
|
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...
|
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
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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: 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
|
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: 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...
|
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,...
| |