I have a collection of arrays which "overlap" at certain elements. Here's a picture of an example involving 3 arrays of characters:
array0↓
'A' ↓array2
array1→'B' 'D' 'E'
'C' 'F'
The important thing is that changes to the arrays should respect this structure. So for example, if I change the 'B' in array0 to 'X', the 'B' in array1 should also change to 'X'.
My question is what is a good, efficient way of implementing this in Python?
There are two things I have thought of so far:
One, I can make a bespoke class, instances of which contain a completely distinct list, along with information about any overlaps that it has, and implement update methods appropriately so that any change to the list is always duplicated for the other lists at the overlaps. This seems a little overwrought though, and involves duplicating the data.
Two, I could do it by using singleton lists like this:
data = [['A'], ['B'], ['C'], ['D'], ['E'], ['F']]
array0 = [data[0], data[1], data[2]]
array1 = [data[1], data[3], data[4]]
array2 = [data[4], data[5]]
for array in array0, array1, array2:
print(array)
>>> [['A'], ['B'], ['C']]
>>> [['B'], ['D'], ['E']]
>>> [['E'], ['F']]
array0[1][0] = 'X'
for array in array0, array1, array2:
print(array)
>>> [['A'], ['X'], ['C']]
>>> [['X'], ['D'], ['E']]
>>> [['E'], ['F']]
But I feel this may be hacky and not the best way. Thanks for any suggestions.
A Python array is a little bit different to arrays in other programming languages in that it uses something called 'list' instead of array. Python 'lists' offer more flexibility than arrays as they can contain different types of data and their length can be varied.
There are different data structures based on hashing, but the most commonly used data structure is the hash table. Hash tables are generally implemented using arrays.
The basic Python data structures in Python include list, set, tuples, and dictionary. Each of the data structures is unique in its own way. Data structures are “containers” that organize and group data according to type. The data structures differ based on mutability and order.
Takeaway. A set is unique in Python. It does not allow duplicates.
Mine suggestion is variation of the one proposed by @a_guest. You could have a wrapper class that marks the elements as shared and a data structure for handling such elements:
class SharedElement:
def __init__(self, val):
self.val = val
def update(self, val):
self.val = val
def __repr__(self):
return "SharedElement({0})".format(self.val)
def __str__(self):
return str(self.val)
class SharedList:
def __init__(self, lst):
self._lst = lst
def __getitem__(self, item):
if isinstance(self._lst[item], SharedElement):
return self._lst[item].val
return self._lst[item]
def __setitem__(self, key, value):
if isinstance(self._lst[key], SharedElement):
self._lst[key].update(value)
B = SharedElement('B')
E = SharedElement('E')
a = SharedList(['A', B, 'C'])
b = SharedList([B, 'D', E])
c = SharedList([E, 'F'])
b[0] = 'X'
print([val for val in a])
print([val for val in b])
print([val for val in c])
Output
['A', 'X', 'C']
['X', 'D', 'E']
['E', 'F']
You can create a wrapper class that can handle the updating of all elements of the same value:
arr = [[['A'], ['B'], ['C']], [['B'], ['D'], ['E']], [['E'], ['F']]]
class WrapArray:
def __init__(self, _data):
self.d = _data
def __getitem__(self, _x):
self.x = _x
class _wrapper:
def __init__(self, _inst):
self.ref = _inst
def __setitem__(self, _y, _val):
_place = self.ref.d[self.ref.x][_y][0]
self.ref.d[self.ref.x][_y][0] = _val
for i in range(len(self.ref.d)):
for b in range(len(self.ref.d[i])):
if self.ref.d[i][b][0] == _place:
self.ref.d[i][b] = [_val]
return _wrapper(self)
def __repr__(self):
return str(self.d)
array = WrapArray(arr)
array[1][0] = 'X'
Output:
[[['A'], ['X'], ['C']], [['X'], ['D'], ['E']], [['E'], ['F']]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With