448,652 Members | 1,698 Online Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

# Lists: Converting Double to Single

 P: n/a I start with a list of tuples retrieved from a database table. These tuples are extracted and put into individual lists. So I have lists that look like this: [1, 2, 3, 4, 5]. When I concatenate lists, I end up with a list of lists that looks like this: [[1, 2, 3. 4, 5]. [6, 7. 8, 9. 10]]. Then, I average the column values so I end up with a single list, but with two brackets on each end, for example, [[3.5, 4.5, 5.5, 6.5, 7.5]]. Unfortunately, when I try to use that last list in a NumPy function, I'm told that it cannot be broadcast to the correct shape. So, what I want to do is strip the extra brackes from each end to leave just [3.5, 4.5, 5.5, 6.5, 7.5]. How do I do this, please? Rich Feb 27 '07 #1
11 Replies

 P: n/a rs******@nospam.appl-ecosys.com wrote: So I have lists that look like this: [1, 2, 3, 4, 5]. When I concatenate lists, I end up with a list of lists that looks like this: [[1, 2, 3. 4, 5]. [6, 7. 8, 9. 10]]. Really? >>[1, 2, 3, 4, 5] + [6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Then, I average the column values so I end up with a single list, but with two brackets on each end, for example, [[3.5, 4.5, 5.5, 6.5, 7.5]]. Unfortunately, when I try to use that last list in a NumPy function, I'm told that it cannot be broadcast to the correct shape. So, what I want to do is strip the extra brackes from each end to leave just [3.5, 4.5, 5.5, 6.5, 7.5]. l = l Feb 27 '07 #2

 P: n/a rs******@nospam.appl-ecosys.com wrote: I end up with a single list, but with two brackets on each end, for example, [[3.5, 4.5, 5.5, 6.5, 7.5]]. Unfortunately, when I try to use that last list in a NumPy function, I'm told that it cannot be broadcast to the correct shape. So, what I want to do is strip the extra brackes from each end to leave just [3.5, 4.5, 5.5, 6.5, 7.5]. How do I do this, please? Easy :) I'll show you by example: >>ham = [[1,2,3],[4,5,6]]ham [[1, 2, 3], [4, 5, 6]] >>ham [1, 2, 3] >>ham.remove([4,5,6])ham [1, 2, 3] >>ham [[1, 2, 3]] >>> HTH&Regards, Björn -- BOFH excuse #453: Spider infestation in warm case parts Feb 27 '07 #3

 P: n/a On 2007-02-27, Leif K-Brooks

 P: n/a Leif K-Brooks wrote: rs******@nospam.appl-ecosys.com wrote: >...Unfortunately, when I try to use that last list in a NumPy function,I'm told that it cannot be broadcast to the correct shape. So, what Iwant to do is strip the extra brackes from each end to leave just[3.5, 4.5, 5.5, 6.5, 7.5]. l = l Or, if you want to simultaneously assert there is only one list, use unpacking like: [l] = l Although I'd prefer to use a better name than l; something like: lst = [[3.5, 4.5, 5.5, 6.5, 7.5]] [inner] = lst print inner, lst -- --Scott David Daniels sc***********@acm.org Feb 27 '07 #5

 P: n/a Dennis Lee Bieber >from numpy import arraylst = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]array(lst).mean(axis=0) array([ 3.5, 4.5, 5.5, 6.5, 7.5]) What I haven't tried to figure out is whether, having said all that, NumPy's mean is any more accurate than the naive sum and divide calculation? So far as I can tell, it isn't any more accurate although it does give different results. Feb 27 '07 #6

 P: n/a On Tue, 27 Feb 2007 10:06:29 +0000, Duncan Booth wrote: Adding up a long list of values and then dividing by the number of values is the classic computer science example of how to get an inaccurate answer from a floating point calculation. I'm not entirely ignorant when it comes to computational mathematics, but I must admit this is a new one to me. If the values vary greatly in magnitude, you probably want to add them from smallest to biggest; other than that, how else can you calculate the mean? The only alternative I thought of was to divide each value by the count before summing, but that would be horribly inaccurate. Or would it? >>def mean1(*args): .... return sum(args)/len(args) .... >>def mean2(*args): .... n = len(args) .... return sum([x/n for x in args]) .... >>L = range(25, 597) # 572 valuesL = [x/3.3 for x in L]mean1(*L) 94.090909090909108 >>mean2(*L) 94.090909090909108 The first calculation has 571 additions and one division; the second calculation has 571 additions and 572 divisions, but they both give the same result to 15 decimal places. -- Steven. Feb 27 '07 #7

 P: n/a On 2007-02-27, Steven D'Aprano Adding up a long list of values and then dividing by thenumber of values is the classic computer science example ofhow to get an inaccurate answer from a floating pointcalculation. I'm not entirely ignorant when it comes to computational mathematics, but I must admit this is a new one to me. If the values vary greatly in magnitude, you probably want to add them from smallest to biggest; other than that, how else can you calculate the mean? The only alternative I thought of was to divide each value by the count before summing, but that would be horribly inaccurate. Or would it? >>>def mean1(*args): ... return sum(args)/len(args) ... >>>def mean2(*args): ... n = len(args) ... return sum([x/n for x in args]) ... >>>L = range(25, 597) # 572 valuesL = [x/3.3 for x in L]mean1(*L) 94.090909090909108 >>>mean2(*L) 94.090909090909108 The first calculation has 571 additions and one division; the second calculation has 571 additions and 572 divisions, but they both give the same result to 15 decimal places. How about this? >>L = range(5, 10)L = [pow(2, -x) for x in L]"%40.40f" % mean1(*L) '0.0121093750000000000000000000000000000000' >>"%40.40f" % mean2(*L) '0.0121093750000000020000000000000000000000' Offhand, I think the first is "righter". Weird! -- Neil Cerutti Feb 27 '07 #8 