Python Forum
Python, how to manage multiple data in list or dictionary with calculations and FIFO - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python, how to manage multiple data in list or dictionary with calculations and FIFO (/thread-35917.html)



Python, how to manage multiple data in list or dictionary with calculations and FIFO - Mikeardy - Dec-29-2021

I need to manage multiple data organized like as following:

SLOT1
| timestamp | value1 | value2 | value3 | value4 |
|-----------|--------|--------|--------|--------|
| xxxxxxxxx | aaaaaa | bbbbbb | cccccc | dddddd |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| xxxxxxxxx | qqqqqq | rrrrrr | ssssss | tttttt |
SLOT2
| timestamp | value1 | value2 | value3 | value4 |
|-----------|--------|--------|--------|--------|
| xxxxxxxxx | aaaaaa | bbbbbb | cccccc | dddddd |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| xxxxxxxxx | qqqqqq | rrrrrr | ssssss | tttttt |
SLOT 'n'
| timestamp | value1 | value2 | value3 | value4 |
|-----------|--------|--------|--------|--------|
| xxxxxxxxx | aaaaaa | bbbbbb | cccccc | dddddd |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| ......... | ...... | ...... | ...... | ...... |
| xxxxxxxxx | qqqqqq | rrrrrr | ssssss | tttttt |



The data structure should not be a Pandas Dataframe but preferibly a native structure without the use of external libraries.

Each single Slot should have a fixed length and new coming data (let say a new row) should be treated with the FIFO logic. so the last coming data sould be appended at the end and the first one should be extracted mainteining fixed the total length of the data structure.

Last but not the least, I need to add an extra column to each slot to perform a kind of computation like

"columnadded" = ("value1" + "value2") / ("value3" + "value4")

thank you for all will help me on this


RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Larz60+ - Dec-29-2021

what have you tried?


RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Gribouillis - Dec-29-2021

For the rotating FIFO, you could use a collections.deque instance. You could also very easily implement a fixed length rotating structure with a list and a pointer, for example
class Rotating:
    def __init__(self, items):
        self.data = list(items)
        self.head = 0
        
    def __iter__(self):
        for i in range(len(self)):
            yield self[i]
    
    def __getitem__(self, i):
        return self.data[(i + self.head) % len(self)]
    
    def __len__(self):
        return len(self.data)
    
    def append(self, item):
        self.data[self.head] = item
        self.head = (self.head + 1) % len(self)
        
    def __str__(self):
        return 'Rotating({})'.format(
            self.data[self.head:] + self.data[:self.head])
    
R = Rotating('abcdef')
print(R)
R.append('g')
print(R)
R.append('h')
print(R)
print(R[0], len(R), R[5])
print(R.data)
Output:
Rotating(['a', 'b', 'c', 'd', 'e', 'f']) Rotating(['b', 'c', 'd', 'e', 'f', 'g']) Rotating(['c', 'd', 'e', 'f', 'g', 'h']) c 6 h ['g', 'h', 'c', 'd', 'e', 'f']



RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Mikeardy - Dec-29-2021

(Dec-29-2021, 12:36 PM)Larz60+ Wrote: what have you tried?
nothing for now, because my experience in this new field for me...


RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Mikeardy - Dec-30-2021

(Dec-29-2021, 01:11 PM)Gribouillis Wrote: For the rotating FIFO, you could use a collections.deque instance. You could also very easily implement a fixed length rotating structure with a list and a pointer, for example
class Rotating:
    def __init__(self, items):
        self.data = list(items)
        self.head = 0
        
    def __iter__(self):
        for i in range(len(self)):
            yield self[i]
    
    def __getitem__(self, i):
        return self.data[(i + self.head) % len(self)]
    
    def __len__(self):
        return len(self.data)
    
    def append(self, item):
        self.data[self.head] = item
        self.head = (self.head + 1) % len(self)
        
    def __str__(self):
        return 'Rotating({})'.format(
            self.data[self.head:] + self.data[:self.head])
    
R = Rotating('abcdef')
print(R)
R.append('g')
print(R)
R.append('h')
print(R)
print(R[0], len(R), R[5])
print(R.data)
Output:
Rotating(['a', 'b', 'c', 'd', 'e', 'f']) Rotating(['b', 'c', 'd', 'e', 'f', 'g']) Rotating(['c', 'd', 'e', 'f', 'g', 'h']) c 6 h ['g', 'h', 'c', 'd', 'e', 'f']

That's amazing but it's intended for one column only, I need more columns and more slots (that basically are not specified in number at the start of the program).


RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Gribouillis - Dec-30-2021

Use rows as items, for example
r = Rotating((('xxx','aaa','bbb','ccc','ddd') for i in range(10)))



RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Mikeardy - Dec-30-2021

(Dec-30-2021, 02:47 PM)Gribouillis Wrote: Use rows as items, for example
r = Rotating((('xxx','aaa','bbb','ccc','ddd') for i in range(10)))

it's not so clear for me.
How can I access to (for example) SLOTn.ROW.COL ?
It seems to me like a 3D array, isn't it?
How to implement this?


RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Gribouillis - Dec-30-2021

Just make a list of Rotating instances
slots = []
for i in range(5):
     slots.append(Rotating((['xxx','aaa','bbb','ccc','ddd'] for i in range(10))))

value = slots[2][3][1]
But we'd like to see your Python code now. As @Larz60+ wrote above, this becomes interesting only if you try something by yourself.


RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - Mikeardy - Dec-31-2021

(Dec-30-2021, 09:03 PM)Gribouillis Wrote: Just make a list of Rotating instances
slots = []
for i in range(5):
     slots.append(Rotating((['xxx','aaa','bbb','ccc','ddd'] for i in range(10))))

value = slots[2][3][1]
But we'd like to see your Python code now. As @Larz60+ wrote above, this becomes interesting only if you try something by yourself.

Sure,
let me try. I will come back with my code.
THanks