problem converting numbers from a file into list to int

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1528 Views - Last Post: 27 June 2016 - 04:37 AM Rate Topic: -----

#1 asya_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 13-January 16

problem converting numbers from a file into list to int

Posted 24 June 2016 - 04:57 AM

I have to make a program that reads numbers from a file, converts the string of numbers to list of int, square each number and print the total sum. I need to have the three functions: toNumbers(), squareEach() and sumList(). The part that I cannot figure out is the toNumbers(nums).

Thanks is advance

def toNumbers(nums):
    
    for i in nums:
        nums[i]= int(i)
        

def squareEach(nums):
    x = 0
    for i in nums:
        nums[x]= i**2
        x += 1

def sumList(nums):
    sumNums = 0
    for i in nums:
        sumNums += i
    
    return sumNums
        

def main():
    # print a description of the program
    print("This program computes the sum of the squares of numbers")
    print("read from the file.")

    # prompt the user to enter the name of the file to be read
    infileName = input("Please, enter the name of the file: ")

    # open the file
    infile = open(infileName, "r")

    
    nums = infile.readlines()

    

    toNumbers(nums) 
          

    squareEach(nums)    
         
    

    # print the sum of the squares of the values in the file
    print("The sum of the squares of the values in the file is", sumList(nums))

    # close the file
    infile.close()

main()

This post has been edited by asya_c: 24 June 2016 - 05:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem converting numbers from a file into list to int

#2 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 08:55 AM

As a pre-processing step, let me just point out that python standards call for snake_case on function names. Unless you have some convincing reason, it's best to follow standards (see PEP-8 for a pretty good python style guide)

Secondly, it's usually best to return a value from a function rather than beating up on a global.

Now, for your actual question the answer is going to depend on the expected input format. Are you expecting comma-separated, or whitespace-separated, or one number per line, or just whatever the user decides to use, or what?

Whichever it is, you can probably use split() to break up the input into tokens, and then map the int function over the resulting list.

Example:
map(int, number_string.split(",")) 


would work if you have comma-separated numbers - but notice that it's pretty brittle, and can be improved. For example it doesn't tolerate "12, 13, 14"
Was This Post Helpful? 0
  • +
  • -

#3 CurlyJoe  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 95
  • Joined: 19-September 13

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 10:44 AM

I would suggest you print "i", and compare input nums to nums after it's changed in the following function as it is screwing up your input. My coding help, don't use "i", "l" or "o" as single digit variable names as they can look like numbers.
def toNumbers(nums):
    
    for i in nums:
        nums[i]= int(i) 
And you can square and sum at the same time if you want
total += each_num**2

This post has been edited by CurlyJoe: 24 June 2016 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 asya_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 13-January 16

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 12:26 PM

def toNumbers(nums):
    map(int, nums.split(" "))    




I tried it before, but I suppose that when I use
nums = infile.readlines()

it saves "nums" as a list, and not a string. So I receive the error:
'list' object has no attribute 'split'

Thank you, I will keep that in mind.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 12:31 PM

So again, what is the input format?
Was This Post Helpful? 0
  • +
  • -

#6 asya_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 13-January 16

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 01:16 PM

The file has a line of numbers separated by spaces, for example:
12 56 78 59
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 01:24 PM

Okay, so it's a single line. Which you can get as a string. Have you tried using split on that string?
Was This Post Helpful? 0
  • +
  • -

#8 asya_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 13-January 16

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 01:59 PM

I tried, but it, but I guess I didn't do it right.

# open the file
    infile = open(infileName, "r")

    nums = infile.readlines()

    toNumbers(nums)


and in the function definition I have
map(int, nums.split(" "))


But the error is:
map(int, nums.split(" "))
AttributeError: 'list' object has no attribute 'split'
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 02:03 PM

Readlines gets you a list of lines. What is in that list?
Was This Post Helpful? 0
  • +
  • -

#10 asya_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 13-January 16

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 02:35 PM

Ok, so the assignment says:

toNumbers(nums)
In this function, nums is a list of strings, each of which represents a number. It modifies each entry in the list by converting it to a number.
How could I convert each string from the list to a number? What I tried, did't work.
Was This Post Helpful? 0
  • +
  • -

#11 CurlyJoe  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 95
  • Joined: 19-September 13

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 04:35 PM

Quote

What is in that list?
The simplest way is to append the values to a new list and return it.
## just an example, not a complete solution (we don't know what the list contains)
list_of_ints=[]
for num_str in list_of_strings:
    if num_str.isdigit():
        list_of_ints.append(int(num_str))
    else:
        print num_str, "is not a number 

This post has been edited by CurlyJoe: 24 June 2016 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#12 asya_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 13-January 16

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 04:56 PM

I tried this before, but I couldn't make it work

def toNumbers(strList):
    nums = []
        
    for x in strList:
        nums.append(int(x))

    return nums


And here is the related part from main
# read the line of numbers from the file and store it as a list of strings
    strList = infile.readlines()
    
    # pass the values of strList to toNumbers() function
    # assign them to the variable nums
    nums = toNumbers(strList)

    # pass the values of the list "nums" to squareEach(nums) function
    squareEach(nums)    

Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 05:00 PM

View Postasya_c, on 24 June 2016 - 06:56 PM, said:

I tried this before, but I couldn't make it work

def toNumbers(strList):
    nums = []
        
    for x in strList:
        nums.append(int(x))

    return nums


How did it fail? What went wrong?
Was This Post Helpful? 0
  • +
  • -

#14 asya_c  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 13-January 16

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 05:03 PM

nums.append(int(x))
ValueError: invalid literal for int() with base 10: '13 34 14 53 56 76'

I receive this error

Here is my full code
# a function to modify each entry in the strList by converting it to a number 
def toNumbers(strList):
    nums = []
        
    for x in strList:
        nums.append(int(x))

    return nums
    
  
def squareEach(nums):
    x = 0
    for i in nums:
        nums[x]= i**2
        x += 1

def sumList(nums):
    sumNums = 0
    for i in nums:
        sumNums += i
    
    return sumNums
        

def main():
    # print a description of the program
    print("This program computes the sum of the squares of numbers")
    print("read from the file.")

    # prompt the user to enter the name of the file to be read
    infileName = input("Please, enter the name of the file: ")

    # open the file
    infile = open(infileName, "r")

    # read the line of numbers from the file and store it as a list of strings
    strList = infile.readlines()
    
    # pass the values of strList to toNumbers() function
    # assign them to the variable nums
    nums = toNumbers(strList)

    # pass the values of the list "nums" to squareEach(nums) function
    squareEach(nums)    
         
    
    # print the sum of the squares of the values in the file
    # pass the values of the list "nums" to sumList(nums) function
    print("The sum of the squares of the values in the file is", sumList(nums))

    # close the file
    infile.close()

main()

Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: problem converting numbers from a file into list to int

Posted 24 June 2016 - 05:18 PM

nums.append(int(x))
ValueError: invalid literal for int() with base 10: '13 34 14 53 56 76'


Okay, now we're getting somewhere.

So, what do you think went wrong here? Obviously, python thinks that the string "13 34 14 53 56 76" does not represent a number. Does this make sense to you? How could you split this up into things that do represent numbers?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2