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)
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    orange_drinkers = int(data_value)
TypeError: int() argument must be a string or a number, not 'list'
>>> int(data_value)
Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    int(data_value)
TypeError: int() argument must be a string or a number, not 'list'
>>> numstring = raw_input(">")
>
>>> type(numlist)
<type 'list'>
>>> b = str(numlist)
>>> c = []
>>> for digit in b:
 c.append (int(digit))
 
Traceback (most recent call last):
  File "<pyshell#47>", line 2, in <module>
    c.append (int(digit))
ValueError: invalid literal for int() with base 10: '['
>>> i = 0
>>> str(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']"
>>> int(x[,numlist])
SyntaxError: invalid syntax
>>> int(x[numlist])
Traceback (most recent call last):
  File "<pyshell#51>", line 1, in <module>
    int(x[numlist])
TypeError: 'int' object has no attribute '__getitem__'
>>> for x in numlist:
 i.append (int(x))
 
Traceback (most recent call last):
  File "<pyshell#54>", line 2, in <module>
    i.append (int(x))
AttributeError: 'int' object has no attribute 'append'
>>> 




  


On Sunday, July 20, 2014 6:21 PM, Wolfgang Maier 
<wolfgang.ma...@biologie.uni-freiburg.de> wrote:
  


On 20.07.2014 22:37, Marc Tompkins wrote:
>
> First of all, I would take advantage of the "with" construction, like so:
>
> with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile:
>      #do stuff with infile
>
> When the block of code inside of the "with" section finishes, Python
> will take care of closing the file for you - even if your program
> encountered an error.  MAJOR advantage over doing it yourself.
>

Yes, definitely, as you have experienced yourself now.

> Second, there's no need to import "string" anymore; all of the
> functions defined in that module are native methods of strings.
> "line" is a string, so you can just do "line.split()".  split()
> returns a list; you can get the first item in that list with
> line.split()[0], the second item with line.split()[1], etc.
>

I fully agree on the string method part. As for the use of 
line.split()[0] and [1], see my comment in the code below.

> Third, free your mind from the shackles of counting how many objects
> are in a list and using that counter as the index of your loops - this
> is not the Python way!  Instead, let Python take care of the counting
> and indexing for you.
>

True (see the earlier suggestion of using enumerate()), but I'm sure 
you'll learn this over time.

> Fourth (and here I differ from some of the others on this list,
> notably Alan G) - I don't like the "while True: / break" paradigm.
> Your mileage may vary, but I find it makes it harder to understand
> what the program is actually trying to achieve.  In your case, you
> read a line from the file, then check whether it contains anything; I
> like to move the readline() to the bottom of the loop.

In my opinion, this is less readable than the original code. I know that 
some people (usually coming from C or other languages with more 
expressive while loops) don't like it, but there's nothing wrong, in 
general, with using while True with break. It's perfectly pythonic.

Direct use of the file object in a for loop has been suggested already 
and THAT's more readable.

>
> Having said all that, here's my suggested version:
>
> state_name = []
> data_value = []
> with open('C:/Python27/egund919/Program1/BOJ_B.txt','r') as infile:
>      line = infile.readline()  # Doing this twice because you
> apparently want to discard the first line...?
>      line = infile.readline()
>      while line:
>          state_name.append(line.split()[0])
>          data_value.append(line.split()[1])

Rewriting the original

            States, OJ = string.split(line)

as

            states, oj = line.split()

is better (and it's remarkable that you used it as a beginner). This 
line unpacks the elements of the list returned by the split method into 
the two variables on the left side.
Besides avoiding repetitive calls to line.split(), it may also help you 
discover problems with your input file format. If there are more than 
two elements in the list, you'll get an error, so you'll know there was 
something in the file you didn't expect. With the line.split()[0] / [1] 
approach, potential other elements in the list (i.e. unexpected line 
content) will be ignored silently.
Once you've learnt that errors in Python are nothing evil, but there to 
help you and once you've read about try/except to catch them, you'll 
appreciate the advantage of the first version.

>          line = infile.readline()

I don't see why repeating infile.readline() three times should be more 
elegant than a nice and clean while True / break.


> print(state_name)
> print(data_value)


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to