Simple list problem that's defeating me!

N

Neil Webster

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

James Mills

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
 
M

Mark Lawrence

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

Bruno Desthuilliers

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)
 
N

Neil Webster

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

Sion Arrowsmith

Mark Lawrence said:
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:])
 
B

Bruno Desthuilliers

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 += 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top