Bagaimana Anda mengisi array baru dengan python?

3000 menjelaskan solusi kode untuk 75 teknologi


import numpy as np array = np.empty(10) array.fill(5)ctrl + cimport numpy as np

memuat modul Numpy untuk Python

.empty

menghasilkan array Numpy kosong dari bentuk yang ditentukan

10

kami membuat array kosong dari 10 elemen

.fill

isi array dengan nilai yang ditentukan

5

kita akan mengisi array kita dengan nilai 5



21 contoh kode Python ditemukan terkait dengan " fill array". Anda dapat memilih yang Anda suka atau memilih yang tidak Anda sukai, dan pergi ke proyek asli atau file sumber dengan mengikuti tautan di atas setiap contoh

def visitFillArrayData(method, dex, instr_d, type_data, block, instr): width, arrdata = instr_d[instr.args[1]].fillarrdata at = type_data.arrs[instr.args[0]] block.loadAsArray(instr.args[0]) if at is arrays.NULL: block.u8(ATHROW) else: if len(arrdata) == 0: # fill-array-data throws a NPE if array is null even when # there is 0 data, so we need to add an instruction that # throws a NPE in this case block.u8(ARRAYLENGTH) block.add(ir.Pop()) else: st, elet = arrays.eletPair(at) # check if we need to sign extend if elet == b'B' or elet == b'Z': arrdata = [util.signExtend(x, 8) & 0xFFFFFFFF for x in arrdata] elif elet == b'S': arrdata = [util.signExtend(x, 16) & 0xFFFFFFFF for x in arrdata] block.fillarraydata(_arrStoreOps.get(elet, AASTORE), st, arrdata) _

def fill_array(self, array, field, add=False, maximize=False): """ Given a full array (for the while image), fill it with the data on the edges. """ self.fix_shapes() for i in xrange(self.n_chunks): for side in ['left', 'right', 'top', 'bottom']: edge = getattr(self, side).ravel()[i] if add: array[edge.slice] += getattr(edge, field) elif maximize: array[edge.slice] = np.maximum(array[edge.slice], getattr(edge, field)) else: array[edge.slice] = getattr(edge, field) return array

def visitFillArrayData(method, dex, instr_d, type_data, block, instr): width, arrdata = instr_d[instr.args[1]].fillarrdata at = type_data.arrs[instr.args[0]] block.loadAsArray(instr.args[0]) if at is arrays.NULL: block.u8(ATHROW) else: if len(arrdata) == 0: # fill-array-data throws a NPE if array is null even when # there is 0 data, so we need to add an instruction that # throws a NPE in this case block.u8(ARRAYLENGTH) block.add(ir.Pop()) else: st, elet = arrays.eletPair(at) # check if we need to sign extend if elet == b'B': arrdata = [util.signExtend(x, 8) & 0xFFFFFFFF for x in arrdata] elif elet == b'S': arrdata = [util.signExtend(x, 16) & 0xFFFFFFFF for x in arrdata] block.fillarraydata(_arrStoreOps.get(elet, AASTORE), st, arrdata)

def visit_fill_array(self, array, value): self.write_ind() array.visit(self) self.write(' = {', data="ARRAY_FILLED") data = value.get_data() tab = [] elem_size = value.element_width # Set type depending on size of elements data_types = {1: 'b', 2: 'h', 4: 'i', 8: 'd'} if elem_size in data_types: elem_id = data_types[elem_size] else: # FIXME for other types we just assume bytes... logger.warning("Unknown element size {} for array. Assume bytes.".format(elem_size)) elem_id = 'b' elem_size = 1 for i in range(0, value.size*elem_size, elem_size): tab.append('%s' % unpack(elem_id, data[i:i+elem_size])[0]) self.write(', '.join(tab), data="COMMA") self.write('}', data="ARRAY_FILLED_END") self.end_ins() _

def visit_fill_array(self, array, value): self.write_ind() array.visit(self) self.write(' = {', data="ARRAY_FILLED") data = value.get_data() tab = [] elem_size = value.element_width if elem_size == 4: for i in range(0, value.size * 4, 4): tab.append('%s' % unpack('i', data[i:i + 4])[0]) else: # FIXME: other cases for i in range(value.size): tab.append('%s' % unpack('b', data[i])[0]) self.write(', '.join(tab), data="COMMA") self.write('}', data="ARRAY_FILLED_END") self.end_ins()

def visitFillArrayData(method, dex, instr_d, type_data, block, instr): width, arrdata = instr_d[instr.args[1]].fillarrdata at = type_data.arrs[instr.args[0]] block.loadAsArray(instr.args[0]) if at is arrays.NULL: block.u8(ATHROW) else: if len(arrdata) == 0: # fill-array-data throws a NPE if array is null even when # there is 0 data, so we need to add an instruction that # throws a NPE in this case block.u8(ARRAYLENGTH) block.u8(POP) else: st, elet = arrays.eletPair(at) # check if we need to sign extend if elet == b'B': arrdata = [util.signExtend(x, 8) & 0xFFFFFFFF for x in arrdata] elif elet == b'S': arrdata = [util.signExtend(x, 16) & 0xFFFFFFFF for x in arrdata] block.fillarraydata(_arrStoreOps.get(elet, AASTORE), st, arrdata) _

def fill_array(array, fill, length): assert length >= array.shape[0], "Cannot fill" if length == array.shape[0]: return array array2 = fill * np.ones((length), dtype=array.dtype) array2[:array.shape[0]] = array return array2

def fill_array(arr, seq): if arr.ndim == 1: try: len_ = len(seq) except TypeError: len_ = 0 arr[:len_] = seq arr[len_:] = 0 else: for subarr, subseq in izip_longest(arr, seq, fillvalue=()): fill_array(subarr, subseq) _

def fill_array(vtk_arr, state, zf): vtk_arr.SetNumberOfComponents(state['numberOfComponents']) vtk_arr.SetNumberOfTuples(state['size']//state['numberOfComponents']) data = zf.read('data/%s' % state['hash']) dataType = arrayTypesMapping[vtk_arr.GetDataType()] elementSize = struct.calcsize(dataType) if vtk_arr.GetDataType() == 12: # we need to cast the data to Uint64 import numpy as np data = np.frombuffer(data, dtype=np.uint32).astype(np.uint64).tobytes() elementSize = 8 vtk_arr.SetVoidArray(data, len(data)//elementSize, 1) vtk_arr._reference = data _

def fill_with_array(self, var, arr): if isinstance(var.owner, Synapses) and var.name == 'delay': # Assigning is only allowed if the variable has been declared in the # Synapse constructor and is therefore scalar if not var.scalar: raise NotImplementedError( 'GeNN does not support assigning to the ' 'delay variable -- set the delay for all' 'synapses (heterogeneous delays are not ' 'supported) as an argument to the ' 'Synapses initializer.') else: # We store the delay so that we can later access it self.delays[var.owner.name] = numpy.asarray(arr).item() elif isinstance(var.owner, NeuronGroup) and var.name == 'lastspike': # Workaround for versions of Brian 2 <= 2.1.3.1 which initialize # a NeuronGroup's lastspike variable to -inf, no longer supported # by the new implementation of the timestep function if arr == -numpy.inf: logger.warn('Initializing the lastspike variable with -10000s ' 'instead of -inf to copy the behaviour of Brian 2 ' 'for versions >= 2.2 -- upgrade Brian 2 to remove ' 'this warning', name_suffix='lastspike_inf', once=True) arr = numpy.array(-1e4) super(GeNNDevice, self).fill_with_array(var, arr)

def fill_array(self, array, field, add=False, maximize=False): """ Given a full array (for the while image), fill it with the data on the edges. """ self.fix_shapes() for i in xrange(self.n_chunks): for side in ['left', 'right', 'top', 'bottom']: edge = getattr(self, side).ravel()[i] if add: array[edge.slice] += getattr(edge, field) elif maximize: array[edge.slice] = np.maximum(array[edge.slice], getattr(edge, field)) else: array[edge.slice] = getattr(edge, field) return array 0

def fill_array(self, array, field, add=False, maximize=False): """ Given a full array (for the while image), fill it with the data on the edges. """ self.fix_shapes() for i in xrange(self.n_chunks): for side in ['left', 'right', 'top', 'bottom']: edge = getattr(self, side).ravel()[i] if add: array[edge.slice] += getattr(edge, field) elif maximize: array[edge.slice] = np.maximum(array[edge.slice], getattr(edge, field)) else: array[edge.slice] = getattr(edge, field) return array 1

def fill_array(self, array, field, add=False, maximize=False): """ Given a full array (for the while image), fill it with the data on the edges. """ self.fix_shapes() for i in xrange(self.n_chunks): for side in ['left', 'right', 'top', 'bottom']: edge = getattr(self, side).ravel()[i] if add: array[edge.slice] += getattr(edge, field) elif maximize: array[edge.slice] = np.maximum(array[edge.slice], getattr(edge, field)) else: array[edge.slice] = getattr(edge, field) return array 2

def fill_array(self, array, field, add=False, maximize=False): """ Given a full array (for the while image), fill it with the data on the edges. """ self.fix_shapes() for i in xrange(self.n_chunks): for side in ['left', 'right', 'top', 'bottom']: edge = getattr(self, side).ravel()[i] if add: array[edge.slice] += getattr(edge, field) elif maximize: array[edge.slice] = np.maximum(array[edge.slice], getattr(edge, field)) else: array[edge.slice] = getattr(edge, field) return array _3

def fill_array(self, array, field, add=False, maximize=False): """ Given a full array (for the while image), fill it with the data on the edges. """ self.fix_shapes() for i in xrange(self.n_chunks): for side in ['left', 'right', 'top', 'bottom']: edge = getattr(self, side).ravel()[i] if add: array[edge.slice] += getattr(edge, field) elif maximize: array[edge.slice] = np.maximum(array[edge.slice], getattr(edge, field)) else: array[edge.slice] = getattr(edge, field) return array _4

def fill_array(self, array, field, add=False, maximize=False): """ Given a full array (for the while image), fill it with the data on the edges. """ self.fix_shapes() for i in xrange(self.n_chunks): for side in ['left', 'right', 'top', 'bottom']: edge = getattr(self, side).ravel()[i] if add: array[edge.slice] += getattr(edge, field) elif maximize: array[edge.slice] = np.maximum(array[edge.slice], getattr(edge, field)) else: array[edge.slice] = getattr(edge, field) return array 5

Apa yang dilakukan fill () dengan Python?

fill() digunakan untuk mengisi array numpy dengan nilai skalar . Jika kita harus menginisialisasi array numpy dengan nilai yang identik maka kita menggunakan numpy. ndarray. mengisi().

Bagaimana Anda menginisialisasi array baru dengan Python?

Untuk menginisialisasi array dengan nilai default, kita dapat menggunakan fungsi for loop dan range() dalam bahasa python. Fungsi Python range() mengambil angka sebagai argumen dan mengembalikan urutan angka mulai dari 0 dan diakhiri dengan angka tertentu, bertambah 1 setiap kali.

Bagaimana cara mengisi array NumPy yang kosong?

Ada dua cara sederhana untuk mengisi larik NumPy. Anda dapat mengisi larik yang ada dengan nilai tertentu menggunakan numpy. isi() . Atau, Anda dapat menginisialisasi array baru dengan nilai tertentu menggunakan numpy.

Bagaimana Anda memperbarui seluruh array dengan Python?

Mereka tersedia di Python dengan mengimpor modul array. Daftar, tipe bawaan di Python, juga mampu menyimpan banyak nilai. .
Operator penugasan untuk mengubah atau memperbarui array
Tambahkan metode () untuk menambahkan satu elemen
Extend() metode untuk menambahkan beberapa item

Postingan terbaru

LIHAT SEMUA