the first element in the list of list

Ben Bush pythonnew at gmail.com
Tue Nov 22 11:02:20 EST 2005


On 11/22/05, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Ben Bush wrote:
>
> > I have a lis:
> > [[1,3],[3,4],[5,6],[8,9],[14,0],[15,8]]
> > 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. that is:
> > [[14,0],[15,8],[1,3],[3,4],[5,6],[8,9]]
>
> have you tried doing this yourself?  how far did you get?  can you
> post the code so we can help you figure out what you need to fix?
> is this a homework assignment?
>
> here's a oneliner:
>
>    L = [x for x in L if x[0]-x[1] >= 6] + [x for x in L if x[0]-x[1] < 6]
>
> depending on how you define "difference", you may need to replace
> x[0]-x[1] with abs(x[0]-x[1])

Thanks! it works!
This question just came to my mind and not assigned by anyone.
I did not know '+' can be used to link the tow lists.
By the way, where i can find more information about how to use List
and data structure in Python.
really amazed by its use!



More information about the Python-list mailing list