From: Neil Webster on 22 Jun 2010 10:06 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.
From: James Mills on 22 Jun 2010 10:30 On Wed, Jun 23, 2010 at 12:06 AM, Neil Webster <nswebster(a)gmail.com> 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"
From: Mark Lawrence on 22 Jun 2010 10:40 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.
From: Bruno Desthuilliers on 22 Jun 2010 11:38 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)
From: Neil Webster on 24 Jun 2010 05:21 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...(a)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)

Next

Last
Pages: 1 2 Prev: Book review / advise Next: Google Adsense Account Approval With In 4 Hours 