[Tutor] (no subject)

LN A-go-go lnartist at yahoo.com
Wed Jul 23 03:36:51 CEST 2014


Hi again and thank-you Wolfgang and company.  I wish I could give you snickers bars or something!  I was able to get through the road_blocks with your help.  I have been working the last few days, I am sorry to say, unsuccessfully, to calculate the mean (that's easy), split the data into sub-groups or secondary means - which are the break values between 4 classes.  Create data-sets with incursive values.  I can do it with brute force (copy and paste) but need to rise to the pythonic way and use a while loop and a nested if-else structure.  My attempts have been lame enough that I don't even want to put them here.
int_list
[36, 39, 39, 45, 61, 54, 61, 93, 62, 51, 47, 72, 54, 36, 62, 50, 41, 41, 40, 62, 62, 58, 57, 54, 49, 43, 47, 50, 45, 41, 54, 57, 57, 55, 62, 51, 34, 57, 55, 63, 45, 45, 42, 44, 34, 53, 67, 58, 56, 43, 33]
>>> int_list.sort()
>>> int_list
[33, 34, 34, 36, 36, 39, 39, 40, 41, 41, 41, 42, 43, 43, 44, 45, 45, 45, 45, 47, 47, 49, 50, 50, 51, 51, 53, 54, 54, 54, 54, 55, 55, 56, 57, 57, 57, 57, 58, 58, 61, 61, 62, 62, 62, 62, 62, 63, 67, 72, 93]
>>> flo_list = [float(integral) for integral in int_list]
>>> flo_list
[33.0, 34.0, 34.0, 36.0, 36.0, 39.0, 39.0, 40.0, 41.0, 41.0, 41.0, 42.0, 43.0, 43.0, 44.0, 45.0, 45.0, 45.0, 45.0, 47.0, 47.0, 49.0, 50.0, 50.0, 51.0, 51.0, 53.0, 54.0, 54.0, 54.0, 54.0, 55.0, 55.0, 56.0, 57.0, 57.0, 57.0, 57.0, 58.0, 58.0, 61.0, 61.0, 62.0, 62.0, 62.0, 62.0, 62.0, 63.0, 67.0, 72.0, 93.0]
>>> sum(flo_list)
2617.0
>>> totalnum = sum(flo_list)
>>> len(flo_list)
51
>>> mean = sum(flo_list)/len(flo_list)
>>> mean
51.31372549019608

Thank-you again,

LN 


On Monday, July 21, 2014 2:55 AM, Wolfgang Maier <wolfgang.maier at biologie.uni-freiburg.de> wrote:
  


On 21.07.2014 01:48, LN A-go-go wrote:> Dear Gurus,
> Thank-you thank-you, one more time, thank-you.
> I am over that hurdle and have the other: converting the list of
> characters that was just created by the split - into integers so I can
> manipulate them.  I was trying to convert to a string, then to
> integers.  I have been scouring the web for casting integers, converting
> to strings and also trying to look up the error messages: I
> apologize for the mess - I was trying everything.
>
> Sigh,
> Python in the hands of the naïve
>
> LN.
>
>>>> numlist = data_value
>>>> numlist
> ['36', '39', '39', '45', '61', '54', '61', '93', '62', '51', '47', '72',
> '54', '36', '62', '50', '41', '41', '40', '62', '62', '58', '57', '54',
> '49', '43', '47', '50', '45', '41', '54', '57', '57', '55', '62', '51',
> '34', '57', '55', '63', '45', '45', '42', '44', '34', '53', '67', '58',
> '56', '43', '33']
>>>> print sum(numlist)
> Traceback (most recent call last):
>    File "<pyshell#38>", line 1, in <module>
>      print sum(numlist)
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>> orange_drinkers = int(data_value)

First thing you have to do in programming is to state your problem 
correctly: you do not want to convert your list to an integer 
representation.
This is what you are attempting in your last line above: convert the 
data_value list to a single integer, and obviously, that cannot work.

Instead, what you want to do is to convert every element in your list to 
an integer. You already know how to do this in a for loop:

int_list = []
for element in data_value:
     int_list.append(int(element))

However, for simple transformations like this, Python has a very 
powerful construct called list comprehensions (read about them here: 
https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
, they're really great), which allow you to replace the for loop with a 
simple:

int_list = [int(element) for element in data_value]

You can even combine the comprehension with sum on one line like this:

print sum([int(element) for element in data_value])

Yet, while all of the above should help you learn Python programming 
patterns, your original code already had the best solution:

data_value = []
..
while True:
     line = infile.readline()
     if not line: break
     ..
     States, OJ = string.split(line)
     XNUM = int(OJ)
     ..
     data_value.append(XNUM)

, i.e. converting each value to an int before adding it to the original 
list.

Best,
Wolfgang

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140722/8d6db70d/attachment.html>


More information about the Tutor mailing list