1,105,177 Community Members

PYTHON: Converting String to Integer!!!

Member Avatar
thompsonSensibl
Light Poster
36 posts since Apr 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi folks, ThompsonSensibl, so I'm new to Python.

This is probably a noob question...

I want to convert a String input to an Integer. I usually do it like this: number = int(someString). However this time this approach doesn't seem to be efficient at all.

So far I have:

#To get number inputs:
print 'Enter the numbers.'
read = sys.stdin.readline()

#Getting the String input (...numbers of type String, not Integer!) into an array
array = read.split()

#Converting String content of array into an Integer.
firstNumber = int(array[0])
secondNumber = int(array[1])
thirdNumber = int(array[2])
...#etc. etc.

As you can see, this conversion process (from String to Integer) is very inefficient as you must convert each cell one by one...

Is there a more efficient alternative solution to this conversion process???


Cheers,
ThompsonSensibl

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

You can write

int_array = [ int(s) for s in array ]
Member Avatar
megaflo
Newbie Poster
5 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Can't you just do this?

for a in array:
    number = int(a)
Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

Can't you just do this?

for a in array:
    number = int(a)

Yes you can, but if you want to do something with the integers, you must store them somewhere.

Member Avatar
megaflo
Newbie Poster
5 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Why? Without knowing the OP use case there may or may not be a reason to store the numbers.

Member Avatar
jcao219
Posting Pro in Training
414 posts since Dec 2009
Reputation Points: 18 [?]
Q&As Helped to Solve: 97 [?]
Skill Endorsements: 1 [?]
 
0
 

Why? Without knowing the OP use case there may or may not be a reason to store the numbers.

No, Gribouillis's code is the most efficient.
There probably is a need to store the numbers, since his original code was something like

firstNumber = int(array[0])
secondNumber = int(array[1])
thirdNumber = int(array[2])

which is definitely storing the numbers.

Member Avatar
megaflo
Newbie Poster
5 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

You can easily do something within the loop iff you need to. Without knowing the use case, who can telll what the OP wants to do? Maybe you are correct, maybe me, but when it comes to computing, I don't like guessing!!!:)

Question Answered as of 3 Years Ago by megaflo, Gribouillis and jcao219
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article