10 examples of 'how to initialize an array in python' in Python

Every line of 'how to initialize an array in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
53def __init__(self, size):
54 assert size > 0, "Array size must be > 0"
55 self._size = size
56 PyArrayType = ctypes.py_object * size
57 self._elements = PyArrayType()
58 self.clear(None)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

13def __init__(self, arr):
14 assert arr is not None
15 self.shape = arr.shape
16 self.array = arr
17 self.size = self.array.size
97def __init__(self, elemtype, *ignored):
98 '''Note: elemcount is ignored when using in the Python scope.
99 '''
100 self.elemtype = elemtype
301def __init__(self, db, ARRAY, varlength=1):
302 self.db = db
303 self.ARRAY = ARRAY
304 self.LLTYPE = ARRAY
305 self.varlength = varlength
306 self.dependencies = {}
307 self.itemtypename = db.gettype(ARRAY.OF, who_asks=self)
215def test_array_instance():
216 LENGTH = 14242
217 p = new_primitive_type("int")
218 p1 = new_array_type(new_pointer_type(p), LENGTH)
219 a = new(p1, None)
220 assert repr(a) == "<cdata 'int[%d]' owning %d bytes>" % (
221 LENGTH, LENGTH * size_of_int())
222 assert len(a) == LENGTH
223 for i in range(LENGTH):
224 assert a[i] == 0
225 py.test.raises(IndexError, "a[LENGTH]")
226 py.test.raises(IndexError, "a[-1]")
227 for i in range(LENGTH):
228 a[i] = i * i + 1
229 for i in range(LENGTH):
230 assert a[i] == i * i + 1
231 e = py.test.raises(IndexError, "a[LENGTH+100] = 500")
232 assert ('(expected %d < %d)' % (LENGTH+100, LENGTH)) in str(e.value)
81def ConstructArray(self, py_arr):
82 ''' note py_arr elems are NOT converted to PyJs types!'''
83 arr = self.NewArray(len(py_arr))
84 arr._init(py_arr)
85 return arr
101def new_array(self):
102 return Value(self.vm, unqlite_vm_new_array(self.vm))
17def __init__(self,
18 *array: Union[np.ndarray, pd.DataFrame, pd.Series,
19 torch.Tensor],
20 dtypes: Union[None, Sequence[torch.dtype]] = None):
21 if dtypes is None:
22 dtypes = [torch.get_default_dtype()] * len(array)
23 if len(dtypes) != len(array):
24 raise ValueError('length of dtypes not equal to length of array')
25
26 array = [
27 self._convert(data, dtype) for data, dtype in zip(array, dtypes)
28 ]
29 super().__init__(*array)
348def __init__(self, db, FIXEDARRAY):
349 self.db = db
350 self.FIXEDARRAY = FIXEDARRAY
351 self.LLTYPE = FIXEDARRAY
352 self.dependencies = {}
353 self.itemtypename = db.gettype(FIXEDARRAY.OF, who_asks=self)
40def init_array(C, A, B, alpha, beta):
41 n = N.get()
42 m = M.get()
43
44 alpha[0] = datatype(1.5)
45 beta[0] = datatype(1.2)
46
47 for i in range(m):
48 for j in range(n):
49 C[i, j] = datatype((i + j) % 100) / m
50 B[i, j] = datatype((n + i - j) % 100) / m
51 for i in range(m):
52 for j in range(i + 1):
53 A[i, j] = datatype((i + j) % 100) / m
54 for j in range(i + 1, m):
55 A[i, j] = -999
56 # regions of arrays that should not be used
57
58 print('aval', beta[0] * C[0, 0] + alpha[0] * B[0, 0] * A[0, 0])

Related snippets