FAQ
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.

My attempt so far is:
L = [[a,2,3,4],[b,10,11,12], [a,2,3,4]]
d = []
z = 1
while z <= len(L):
for a in L:
if L.count(a[0]) > 1:
d.append(a[2:])

summed = [sum(pair) for pair in zip(d[0], d[1])]
z = z+1
print summed

Any pointers more than welcome.

Thanks all.

Search Discussions

•  at Jun 22, 2010 at 2:21 pm ⇧

On 23 June 2010 00:06, Neil Webster 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.html>
•  at Jun 22, 2010 at 2:30 pm ⇧

On Wed, Jun 23, 2010 at 12:06 AM, Neil Webster wrote:
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?
Is this a hypothetical/mathematical problem of sorts ?
If so, do you have the actual problem description ?

It's often better to come up with a different (perhaps better)
solution without looking at someone else's :)

--james

--
--
-- "Problems are solved by method"
•  at Jun 22, 2010 at 2:40 pm ⇧

On 22/06/2010 15:06, Neil Webster 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.

My attempt so far is:
L = [[a,2,3,4],[b,10,11,12], [a,2,3,4]]
d = []
z = 1
while z<= len(L):
for a in L:
if L.count(a[0])> 1:
d.append(a[2:])

summed = [sum(pair) for pair in zip(d[0], d[1])]
z = z+1
print summed

Any pointers more than welcome.

Thanks all.
My simplistic approach.

Sort the list (this happens in place).
Use the itertools groupby function to place everything together, see
http://docs.python.org/library/itertools.html?highlight=groupby#itertools.groupby
Combine the lists in the groups.

HTH.

Mark Lawrence.
•  at Jun 22, 2010 at 3:38 pm ⇧

Neil Webster a ?crit :
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
1/ not posting working code (got a NameError)
2/ not posting the expected output
3/ not posting the actual output
or offer an alternative
solution to the problem?
When you'll have fixed the 3 problems listed above !-)

(snip broken code)
•  at Jun 24, 2010 at 9:21 am ⇧
Thanks for the help so far.

The background to the problem is that the lists come from reading a
dbf file. The code that I am trying to write is to merge lines of the
dbf based on the first column. So in my example there would be three
lines:
a 2 3 4
b 10 11 12
a 2 3 4

The expected output from the above example lines would be:
a 4 6 8
b 10 11 12

... and the lines are read as: [[a,2,3,4],[b,10,11,12], [a,2,3,4]]

In response to not posting working code or actual inputs, ummm, that's
why I am asking the question here.

On Jun 22, 4:38?pm, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
Neil Webster a crit :
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
1/ not posting working code (got a NameError)
2/ not posting the expected output
3/ not posting the actual output
or offer an alternative
solution to the problem?
When you'll have fixed the 3 problems listed above !-)

(snip broken code)
•  at Jun 24, 2010 at 11:45 am ⇧

Neil Webster a ?crit :
Thanks for the help so far.

The background to the problem is that the lists come from reading a
dbf file. The code that I am trying to write is to merge lines of the
dbf based on the first column. So in my example there would be three
lines:
a 2 3 4
b 10 11 12
a 2 3 4

The expected output from the above example lines would be:
a 4 6 8
b 10 11 12

... and the lines are read as: [[a,2,3,4],[b,10,11,12], [a,2,3,4]]
If you don't care about the original ordering, the following code should
do.

# 8<----------------------------------------------------------------

def merge_rows(rows):
merged = dict()
for row in rows:
key, values = row[0], row[1:]
sums = merged.setdefault(key, [0, 0, 0])
for i, v in enumerate(values):
sums[i] += v

return [key] + value for key, value in merged.iteritems()]

import sys
def print_rows(rows, out=sys.stdout):
for row in rows:
print " ".join(map(str, row))

if __name__ == '__main__':
inputs = [['a',2,3,4],['b',10,11,12], ['a',2,3,4]]
expected = [['a',4,6,8],['b',10,11,12]]
print "inputs : "
print_rows(inputs)

outputs = merge_rows(inputs)
outputs.sort() # so we can do a simple equality test
assert outputs == expected, "Expected %s, got %s" % (
expected, outputs
)

print "outputs :"
print_rows(outputs)

# 8<----------------------------------------------------------------

In response to not posting working code or actual inputs, ummm, that's
why I am asking the question here.
In this context, "working code" means "minimal code that a) can be
executed and b) exhibits the problem you have, whatever the problem is".
This code should include or be shipped with example input data and
corresponding expected output data - as I did in the above code.

HTH
•  at Jun 24, 2010 at 9:24 am ⇧

Mark Lawrence wrote:
On 22/06/2010 15:06, Neil Webster wrote:
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.
[ ... ]
My simplistic approach.

Sort the list (this happens in place).
Use the itertools groupby function to place everything together, see
http://docs.python.org/library/itertools.html?highlight=groupby#itertools.groupby
Combine the lists in the groups.
I suspect the following is a more efficient way of acheiving the grouping:

d = collections.defaultdict(list)
for a in L:
d[a[0]].append(a[1:])

--
\S

under construction

Related Discussions

Discussion Overview
 group python-list categories python posted Jun 22, '10 at 2:06p active Jun 24, '10 at 11:45a posts 8 users 6 website python.org

6 users in discussion

Content

People

Support

Translate

site design / logo © 2022 Grokbase