11 KiB
11 KiB
In [ ]:
L = list(range(10))
LIn [ ]:
type(L[0])In [ ]:
L2 = [str(c) for c in L]
L2In [ ]:
type(L2[0])In [ ]:
L3 = [True, "2", 3.0, 4]
[type(item) for item in L3]In [ ]:
import array
L = list(range(10))
A = array.array('i', L)
AIn [ ]:
import numpy as npIn [ ]:
# Integer array
np.array([1, 4, 2, 5, 3])In [ ]:
np.array([3.14, 4, 2, 3])In [ ]:
np.array([1, 2, 3, 4], dtype=np.float32)In [ ]:
# Nested lists result in multidimensional arrays
np.array([range(i, i + 3) for i in [2, 4, 6]])In [ ]:
# Create a length-10 integer array filled with 0s
np.zeros(10, dtype=int)In [ ]:
# Create a 3x5 floating-point array filled with 1s
np.ones((3, 5), dtype=float)In [ ]:
# Create a 3x5 array filled with 3.14
np.full((3, 5), 3.14)In [ ]:
# Create an array filled with a linear sequence
# starting at 0, ending at 20, stepping by 2
# (this is similar to the built-in range function)
np.arange(0, 20, 2)In [ ]:
# Create an array of five values evenly spaced between 0 and 1
np.linspace(0, 1, 5)In [ ]:
# Create a 3x3 array of uniformly distributed
# pseudorandom values between 0 and 1
np.random.random((3, 3))In [ ]:
# Create a 3x3 array of normally distributed pseudorandom
# values with mean 0 and standard deviation 1
np.random.normal(0, 1, (3, 3))In [ ]:
# Create a 3x3 array of pseudorandom integers in the interval [0, 10)
np.random.randint(0, 10, (3, 3))In [ ]:
# Create a 3x3 identity matrix
np.eye(3)In [ ]:
# Create an uninitialized array of three integers; the values will be
# whatever happens to already exist at that memory location
np.empty(3)