what is new with int conversion in Python 3
Sayth Renshaw
flebber.crue at gmail.com
Sun May 22 03:26:39 EDT 2016
I am doing a passage in a book that was written for python 2 i am writing everything in 3.
This is the author Ivan Idris code to show time difference between python and numpy arrays. The only edit I made was to fix the print statements.
#!/usr/bin/env/python
import sys
from datetime import datetime
import numpy as np
"""
This program demonstrates vector addition the Python way.
Run from the command line as follows
python vectorsum.py n
where n is an integer that specifies the size of the vectors.
The first vector to be added contains the squares of 0 up to n.
The second vector contains the cubes of 0 up to n.
The program prints the last 2 elements of the sum and the elapsed time.
"""
def numpysum(n):
a = np.arange(n) ** 2
b = np.arange(n) ** 3
c = a + b
return c
def pythonsum(n):
a = range(n)
b = range(n)
c = []
for i in range(len(a)):
a[i] = i ** 2
b[i] = i ** 3
c.append(a[i] + b[i])
return c
size = int(sys.argv[1])
start = datetime.now()
c = pythonsum(size)
delta = datetime.now() - start
print("The last 2 elements of the sum", c[-2:])
print("PythonSum elapsed time in microseconds", delta.microseconds)
start = datetime.now()
c = numpysum(size)
delta = datetime.now() - start
print("The last 2 elements of the sum", c[-2:])
print("NumPySum elapsed time in microseconds", delta.microseconds)
However when I run this I get a valuerror. So either something has changed with int or datetime I cannot google a consistent answer.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-a54a878f293d> in <module>()
37 return c
38
---> 39 size = int(sys.argv[1])
40
41 start = datetime.now()
ValueError: invalid literal for int() with base 10: '-f'
Had this before?
Sayth
More information about the Python-list
mailing list