[melbourne-pug] array matching

N6151H n6151h at gmail.com
Fri Apr 30 10:59:17 CEST 2010


In general, I agree with you that code should be readable.  On the other
hand, we don't necessarily want to be writing for the lowest common
denominator, i.e., the newcomer.

There's room for abuse in any language, but I don't consider any particular
construct (or language) "evil" per se.   Otherwise, you can extend that
appellation to anything beyond pure machine code since, for the "newcomer"
symbolic instruction sets and higher-level, algebraic languages and even
macro-assemblers reduce what's really going on to "one-liners".  You might
see the in-line "for" construct as coming from perl, but in fact it's been
around MUCH longer than that, going back to  list-directed I/O in FORTRAN
IV.

What I think is good, here, is that we've managed to show the questioner
that there are two ways to solve the problem, discuss the relative merits of
both, concluding that  yours is, indeed, the more computationally efficient
approach.  The nice by-product is that it provokes thought as to what may be
lacking in the language that would otherwise allow the one-liner approach to
be used.

Cheers,
Nick

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

>
>
> On 30 April 2010 18:35, N6151H <n6151h at gmail.com> wrote:
>
>> 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
>
>
> Sorry, you're right, that ".items()" shouldn't be there.  I added it in a
> fit of stupidity.
>
> I really dislike the option you gave because one line expressions are evil.
>  They're fine for the initial write, they're hell on the new guy in the
> company trying to upkeep your code, and they're horrible to have to debug
> later on.  Having maintained the same body of python code for over 10 years
> (yes, it was initially written under python 1.x), I would rate readability a
> higher requirement than brevity.  Otherwise, you'd be better off in perl,
> right? :)
>
> I don't think offering one-liners to newcomers is wise - at least, not
> without also giving them a longhand version.  I particularly think if you're
> going to do it as a one-liner, you want to make sure you're gaining
> something for the sacrificed readability - in the case provided, your one
> liner was also less efficient than doing it longhand.  That seems to often
> be the case in Python - write it out naturally, and it's likely to be
> efficient anyway.
>
> But, you're right, the .items() was a typo ;)
>
> KJL
>
>
>> 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
>>>
>>>
>>
>> _______________________________________________
>> 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/b6dd4846/attachment-0001.html>


More information about the melbourne-pug mailing list