En Fri, 13 Mar 2009 17:33:51 -0200, Hrvoje Niksic
escribió:
"andrew cooke" writes:
Hrvoje Niksic wrote:
Kottiyath writes:
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]
Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should
> If you don't want to build the intermediary dict, a
> less efficient
> version that runs in O(n^2):
>
> a.sort(key=lambda k: b.index(k[1]))
>
> Which is mostly similar to John's solution, but still
> more efficient
> because it only does a b.index call once per 'a'
> item instead of twice
> pe
"andrew cooke" writes:
> Hrvoje Niksic wrote:
>> Kottiyath writes:
>>
>>> Hi,
>>> I have 2 lists
>>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
>>> b = [2, 4, 1, 3]
>>>
>>> Now, I want to order _a_ (a[1]) based on _b_.
>>> i.e. the second element in tuple should be the same as b.
>>> i
MRAB wrote:
> >>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
> >>> b = [2, 4, 1, 3]
> >>> d = dict((v, k) for k, v in a)
> >>> c = [(d[s], s) for s in b]
> >>> c
> [(3, 2), (2, 4), (4, 1), (7, 3)]
ah, that is more efficient than the suggestions i posted.
andrew
--
http://mail.python.org/mailman/
Hrvoje Niksic wrote:
> Kottiyath writes:
>
>> Hi,
>> I have 2 lists
>> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
>> b = [2, 4, 1, 3]
>>
>> Now, I want to order _a_ (a[1]) based on _b_.
>> i.e. the second element in tuple should be the same as b.
>> i.e. Output would be [(3, 2), (2, 4),
Kottiyath writes:
> Hi,
> I have 2 lists
> a = [(4, 1), (7, 3), (3, 2), (2, 4)]
> b = [2, 4, 1, 3]
>
> Now, I want to order _a_ (a[1]) based on _b_.
> i.e. the second element in tuple should be the same as b.
> i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]
[...]
> whe
MRAB wrote:
Kottiyath wrote:
Hi,
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]
Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should be the same as b.
i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]
I did the same
Kottiyath wrote:
Hi,
I have 2 lists
a = [(4, 1), (7, 3), (3, 2), (2, 4)]
b = [2, 4, 1, 3]
Now, I want to order _a_ (a[1]) based on _b_.
i.e. the second element in tuple should be the same as b.
i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)]
I did the same as follows: