[melbourne-pug] array matching

N6151H n6151h at gmail.com
Fri Apr 30 10:35:42 CEST 2010


Sorry.  Should have included l1, which is:  [['a', 1], ['b', 2], ['a', 3],
['b', 4]]
Same as what the original poster wanted (substituting strings for the A and
B objects, WoLOG.

Your example presumes test to be an already-populated dict, I think, which
is, to my understanding, where the original poster wanted to end up.  He's
starting with a list.  So, you need to build a dict as you iterate through
that list, preferrably just once.  The method I suggested does indeed
require multiple passes.  The key problem seems to be how to set the initial
value for each key to an empty list, which can then be appended.   You can
do it in one pass like this:

    d = {}
    for k,v in l1:
        d.setdefault(k, []).append(v)

 which is probably what you meant to write, rather than test.items(), I
suspect.

But, it would still be nice to get this down to a one-line expression,
wouldn't it?

Would be nice if you had a form of the reduce built-in function that worked
with something other than scalars.  Then you could write:

    e = {}
    e = reduce (lambda k,v: e.setdefault(k,[]).append(v), l1, e)


How about

On Fri, Apr 30, 2010 at 6:00 PM, Kevin Littlejohn <darius at obsidian.com.au>wrote:

> 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
>
> _______________________________________________
> 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/4a5f3ef1/attachment-0001.html>


More information about the melbourne-pug mailing list