Isi array dengan angka python

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

Bagaimana Anda mengisi array dengan angka di Python?

Kita dapat menggunakan Fungsi Numpy fill() digunakan untuk mengisi array dengan nilai tertentu. Fungsi ini mengembalikan array yang diisi dengan nilai 3 menggunakan arr. mengisi() fungsi.

Bagaimana cara mengisi array dengan nilai NumPy?

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 menyimpan angka dalam array dengan Python?

Kita dapat menambahkan nilai ke array dengan menggunakan fungsi append(), extend() dan insert (i,x) . Fungsi append() digunakan ketika kita perlu menambahkan satu elemen di akhir array. Larik yang dihasilkan adalah larik aktual dengan nilai baru yang ditambahkan di bagian akhir.

Bagaimana Anda mengisi array dengan nilai yang sama?

Daftar Penginisialisasi. Untuk menginisialisasi array di C dengan nilai yang sama, cara naifnya adalah menyediakan daftar penginisialisasi . Kami menggunakan ini dengan array kecil. int jumlah[5] = {1, 1, 1, 1, 1}; .