Python Forum
Python, how to manage multiple data in list or dictionary with calculations and FIFO
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python, how to manage multiple data in list or dictionary with calculations and FIFO
#1
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
Reply
#2
what have you tried?
Reply
#3
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']
Reply
#4
(Dec-29-2021, 12:36 PM)Larz60+ Wrote: what have you tried?
nothing for now, because my experience in this new field for me...
Reply
#5
(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).
Reply
#6
Use rows as items, for example
r = Rotating((('xxx','aaa','bbb','ccc','ddd') for i in range(10)))
Reply
#7
(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?
Reply
#8
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.
Reply
#9
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 416 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 406 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 561 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 695 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  python convert multiple files to multiple lists MCL169 6 1,566 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Sort a list of dictionaries by the only dictionary key Calab 1 497 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 1,012 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  python manage variables across project level mg24 1 923 Nov-12-2022, 05:01 AM
Last Post: deanhystad
  How do you manage script? kucingkembar 14 2,732 Oct-15-2022, 06:32 PM
Last Post: Gribouillis
  Load multiple Jason data in one Data Frame vijays3 6 1,559 Aug-12-2022, 05:17 PM
Last Post: vijays3

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020