Beyond assigning borders to python array - am I inventing a wheel?

I need to create an array, but I get the data out of order (and I don’t know what the highest index of the array will be), so I need to do array[index]=itemwhen the index often goes out of bounds.

I quickly shifted this function, which does what I want, but I feel that there may be an easier way.

def oob_assign(array,index,item,default):
  "set array[index] to item. if index is out of bounds, array is extended as necessary using default"
  array.extend([default]*(index-len(array)+1))
  array[index]=(item)

So for example:

In [4]: a=[]

In [5]: oob_assign(a,5,"five",0)

In [6]: a
Out[6]: [0, 0, 0, 0, 0, 'five']

In [7]: a[5]
Out[7]: 'five'

Edit: while my final goal is too much to ask in the stackoverflow question, the operations I need to do (relatively quickly) on the resulting data:

  • iterate over values ​​in index order (not default - just fine)
  • search values ​​by index

The data set is small enough (~ 1000 elements) that using memory in an array is not a problem.

: ! < 3 Stackoverflow:)

+4
3

(index, data_value) append? :

data_items = []
for index, data_value in generate_out_of_order_data():
    data_items.append((index, data_value))
data_items.sort()
indices, data = zip(*data_items)

:

In [1]: random_data = [(4, 'd'), (1, 'a'), (3, 'c'), (2, 'b')]
In [2]: data_items = []
In [3]: for index, data_value in random_data:
   ...:     data_items.append((index, data_value))
   ...: 
In [4]: data_items.sort()
In [5]: indices, data = zip(*data_items)
In [6]: indices
Out[6]: (1, 2, 3, 4)
In [7]: data
Out[7]: ('a', 'b', 'c', 'd')
+2

:

class OOBList(list):
    def __init__(self, default, *args, **kwargs):
        super(OOBList, self).__init__(*args, **kwargs)
        self.default = default

    def __setitem__(self, index, value):
        max_index = len(self) - 1
        if index > max_index:
            self.extend([self.default] * (index - max_index))
        super(OOBList, self).__setitem__(index, value)

, , .

list, ( ).

+2

( , 50, 25, 1 25), - dict, " " int.

. , list , , , , ..

defaultdict, , . A defaultdict , , len, , , . ... , defaultdict , ; , , , , ...

list -like, collections.abc.MutableSequence .

class ExpandoList(collections.abc.MutableSequence):
    def __init__(self):
        self.d = {}
        self.maxidx = -1
    def __setitem__(self, idx, value):
        self.maxidx = max(idx, self.maxidx)
        self.d[idx] = value
    def __getitem__(self, idx):
        self.maxidx = max(idx, self.maxidx)
        return self.d.get(idx, 0)
    def __delitem__(self, idx):
        for i in range(i, self.maxidx):
            self.d[i] = self.d[i-1]
        self.maxidx -= 1
    def insert(self, idx, value):
        for i in reversed(range(i, self.maxidx)):
            self.d[i] = self.d[i-1]
        self.d[idx] = value
        self.maxidx += 1
    def __iter__(self):
        yield from (self[i] for i in range(self.maxidx))
    def __len__(self):
        return self.maxidx

Please note that this does not cut. You can handle this manually, but it is becoming more complex than I would like to answer. If you want to take this further, I have a class lazylistthat I can load somewhere that shows how to solve the same problems that you need to solve.

+1
source

All Articles