[melbourne-pug] array matching

Nick Farrell nick at farrell.name
Fri Apr 30 09:30:43 CEST 2010


.. or if you're prepared to sacrifice a little brevity for transparency:

>>> s = [['A', 1], ['B', 2], ['A', 3], ['B', 4]]
>>> t = collections.defaultdict(list)
>>> for k, v in s: t[k].append(v)
...
>>> t
defaultdict(<type 'list'>, {'A': [1, 3], 'B': [2, 4]})



On Fri, Apr 30, 2010 at 5:08 PM, N6151H <n6151h at gmail.com> wrote:

> I believe this is what you want:
>
>    dict([(r[0], [s[1] for s in l1 if s[0] == r[0]]) for r in l1])
>
> returns
>
>    {'a': [1, 3], 'b': [2, 4]}
>
> Cheers,
> Nick
>
> On Fri, Apr 30, 2010 at 3:58 AM, Bill Jordan <billjordan121 at yahoo.com>wrote:
>
>> Hey guys,
>>
>> I am sorry if this is not the right list to post some questions. I have a
>> simple question please and would appreciate some answers as I am new to
>> Python.
>>
>> I have 2 D array: test = [[A,1],[B,2],[A,3][B,4]]
>> I want to arrang this array in different arrays so each one will have what
>> is attached to. For example I want the output:
>>
>> A =[1,3] and B=[2,4]
>>
>> Thanks,
>> Bill
>>
>>
>> _______________________________________________
>> melbourne-pug mailing list
>> melbourne-pug at python.org
>> http://mail.python.org/mailman/listinfo/melbourne-pug
>>
>>
>
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> http://mail.python.org/mailman/listinfo/melbourne-pug
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20100430/4cc416ee/attachment.html>


More information about the melbourne-pug mailing list