another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))

sajuptpm sajuptpm at gmail.com
Tue Sep 7 10:22:02 EDT 2010


On Sep 7, 7:03 pm, Peter Otten <__pete... at web.de> wrote:
> sajuptpm wrote:
> > i need to implement  l.sort(key=lambda x:(x[0][0], -x[1][0])) in
> > another way .I want to know what the modification needed in the 'if'
> > check to sort this list of tuples in k[0][0] ascending and k[0][1]
> > descending.
>> It seems you are not getting any closer to your goal. Perhaps it would help
> if you could explain that goal clearly rather than describing the means you
> are employing to achieve it.
>> > I have a list of tuples.
>> > l = [((30,50),(70)), ((50,20),(20))]
>> By the way, (42) is not a tuple, it's an integer. To turn it into a 1-tuple
> you have to add a ',':
>> >>> (42)
> 42
> >>> (42,)
> (42,)
> >>> 42,
>> (42,)
>> Peter

I have a list of tuples.
l = [((30,50),(70,)), ((50,20),(20,))]
for i in range(10):
 k = ((i+30,i+50),(i+70))#suppose creating new tuple in each
iteration
using some random value and in sert it into list.
 flag=True
 for i, v in enumerate(l):
 if v >= k:
 l.insert(i,k)
 flag = False
 break
 if flag:
 l.append(k)
This code will give a list of tuples sorted in asc order.
I need to change this code to sort this list by k[0][0] ascending and
k[0][1] descending.
What are the modifications needed in this code.
I dont want to user sort() method of list.
i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in
another way .I want to know what the modification needed in the 'if'
check to sort this list of tuples in k[0][0] ascending and k[0][1]
descending.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /