9 examples of 'how to create an empty array in python' in Python

Every line of 'how to create an empty 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
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) == "" % (
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
12def create_array():
13 """Creates a simple 3D numpy array with unique values at each
14 location in the matrix.
15
16 """
17 rows, cols, depth = 2, 3, 4
18 arr = numpy.zeros((rows, cols, depth), 'i')
19 count = 0
20 for i in range(rows):
21 for j in range(cols):
22 for k in range(depth):
23 arr[i,j,k] = count
24 count += 1
25 return arr
11def create_empty_arrays():
12 oneD_empty = np.empty(5)
13 twoD_empty = np.empty((5,4))
14 threeD_empty = np.empty((5,4,3))
15
16 print('1D array')
17 print(oneD_empty)
18
19 print('2D array')
20 print(twoD_empty)
21
22 print('3D array')
23 print(threeD_empty)
101def new_array(self):
102 return Value(self.vm, unqlite_vm_new_array(self.vm))
20@unwrap_spec(typecode='text')
21def w_array(space, w_cls, typecode, __args__):
22 if len(__args__.arguments_w) > 1:
23 raise oefmt(space.w_TypeError, "array() takes at most 2 arguments")
24 if len(typecode) != 1:
25 raise oefmt(space.w_TypeError,
26 "array() argument 1 must be char, not str")
27 typecode = typecode[0]
28
29 if space.is_w(w_cls, space.gettypeobject(W_ArrayBase.typedef)):
30 if __args__.keywords:
31 raise oefmt(space.w_TypeError,
32 "array.array() does not take keyword arguments")
33
34 for tc in unroll_typecodes:
35 if typecode == tc:
36 a = space.allocate_instance(types[tc].w_class, w_cls)
37 a.__init__(space)
38 break
39 else:
40 raise oefmt(space.w_ValueError,
41 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or "
42 "d)")
43
44 if len(__args__.arguments_w) > 0:
45 w_initializer = __args__.arguments_w[0]
46 w_initializer_type = space.type(w_initializer)
47 if w_initializer_type is space.w_bytes:
48 a.descr_fromstring(space, w_initializer)
49 elif w_initializer_type is space.w_list:
50 a.descr_fromlist(space, w_initializer)
51 else:
52 a.extend(w_initializer, True)
53 return a
503def test_php_empty_array_len_in_python(self, php_space):
504 output = self.run('''
505 $src = "def f(a): return len(a)";
506 $f = compile_py_func($src);
507 $in = array();
508 echo($f($in));
509 ''')
510 assert php_space.int_w(output[0]) == 0
212def cArray (self, arrTy, arrSize = None):
213 ret_val = {"ty" : "array", "elemty" : arrTy, "array" : arrSize}
214 return ret_val
85def test_empty_array(self):
86 out = self.run("""$x = [];
87 print_r($x);""")
88 assert out == "Array\n(\n)\n"

Related snippets