Simple list problem that's defeating me!
Xavier Ho
contact at xavierho.com
Tue Jun 22 10:21:44 EDT 2010
On 23 June 2010 00:06, Neil Webster <nswebster at gmail.com> wrote:
> Hi all,
>
> I've got a simple problem but it's defeated me and I was wondering if
> somebody could point out where I'm going wrong or offer an alternative
> solution to the problem?
>
> I have a list of lists such as [[a,2,3,4],[b,10,11,12], [a,2,3,4]]. I
> need to combine the two lists that have the same first character in
> this example 'a'. In reality there are 656 lists within the list.
>
Here's my take:
>>> input = [['a',2,3,4], ['b',10,11,12], ['a',2,3,4]]
>>> output = {}
>>> for i in input:
... output[i[0]] = output.get(i[0], 0) + sum(i[1:])
...
>>> output
{'a': 18, 'b': 33}
Note in your example, a and b are not defined, so I used a string instead.
Cheers,
Xav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100623/da5f7a19/attachment-0001.html>
More information about the Python-list
mailing list