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
On Mar 14, 5:39 am, Chris Rebert wrote:
> On Fri, Mar 13, 2009 at 5:30 PM, Peter Pearson
> wrote:
> > On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic
> > wrote:
> > [snip]
> >> a.sort(key=lambda (x, y): b[y - 1], reverse=True)
>
> > Huh? I had no idea one could do this:
>
> def g( ( (
On Fri, Mar 13, 2009 at 5:30 PM, Peter Pearson wrote:
> On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic wrote:
> [snip]
>> a.sort(key=lambda (x, y): b[y - 1], reverse=True)
>
> Huh? I had no idea one could do this:
>
def g( ( ( x, y ), z ) ):
> ... return y
> ...
g( ((1,2),3) )
> 2
On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic wrote:
[snip]
> a.sort(key=lambda (x, y): b[y - 1], reverse=True)
Huh? I had no idea one could do this:
>>> def g( ( ( x, y ), z ) ):
... return y
...
>>> g( ((1,2),3) )
2
What should I have read to learn that trick?
--
To email me, substi
> 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:
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:
>>> l = len(a) * [N
12 matches
Mail list logo