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
#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


Messages In This Thread
RE: Python, how to manage multiple data in list or dictionary with calculations and FIFO - by Gribouillis - Dec-29-2021, 01:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  (Fix it) Extend list with multiple other ones andreimotin 3 774 Aug-12-2024, 07:48 PM
Last Post: Pedroski55
  Sort a list of dictionaries by the only dictionary key Calab 2 1,447 Apr-29-2024, 04:38 PM
Last Post: Calab
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,661 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 1,117 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 1,397 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 1,733 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  python convert multiple files to multiple lists MCL169 6 3,290 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  How to add list to dictionary? Kull_Khan 3 1,800 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  python manage variables across project level mg24 1 1,437 Nov-12-2022, 05:01 AM
Last Post: deanhystad
  How do you manage script? kucingkembar 14 5,637 Oct-15-2022, 06:32 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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