[melbourne-pug] array matching

Kevin Littlejohn darius at obsidian.com.au
Fri Apr 30 10:00:46 CEST 2010


Suspect you can do it more efficiently with more lines:

d = {}
for key, val in test.items():
    d.setdefault(key, []).append(val)

Saves multiple runs through the original list (which I presume is l1 in your
example).  Also makes for more readable code.  d comes out the same as your
example's return val.

KJL

On 30 April 2010 17:08, 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
>
>


-- 
Kevin Littlejohn
Obsidian Consulting Group
ph: +613 9355 7844
skype: silarsis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20100430/868979d1/attachment.html>


More information about the melbourne-pug mailing list