Python String to Integer Conversion

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


27 points

Hi folks.

I want to convert a String input to an Integer.

So far I have:

print 'Enter the numbers.'
read = sys.stdin.readline()
array = read.split()

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

Problem with this is that I think it's rather inefficient, as you must convert each cell one by one...

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

Cheers,
ThompsonSensibl



7 points

Use following to create array of integers:

array = [int(item) for item in read.split()]

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.