Reputation: 565
finding whether all elements in the list are same or not based on the following scenario:-
I am getting price value at every 5 min interval.for first 20 min. I have to simply store these value somewhere for further comparison. I am creating list for that purpose:
initPrice=[] // create empty list
after that, I am appending values to list by using:
initPrice.append(someValue)
so till 20 min, means 4 values will be appended to list. like:-
initPrice=[20,23,20,40]
Now I have to check whether all elements in the list are same or not. so I did-
if(len(set(initprice))<2):
print("TRUE")
else:
print("FALSE")
but the scenario is like live streaming data, so after every 20 min, I want to do the same task again and again on the list, without increasing the length size. its more look like queue first in first out,so after every 20 min, I am doing the same task on updated value. the only problem with list.append is it will increase the list size, that i dont want.
it's like after every 5 min new value should store at one end and older value should remove from another end, that's why i said its more look like FIFO mechanisms
Upvotes: 1
Views: 112
Reputation: 30534
You can add to the list only if the value does not exist:
if someValue not in initPrice:
initPrice.append(someValue)
And use initPrice.pop()
to remove to first value appended to the list.
If you do not need an order, consider using a set instead. See also: Does Python have an ordered set?
Upvotes: 1
Reputation: 15397
Python lists are naturally made to be a stack - they support the pop
function. So if you wanted a stack, it would be simple:
>>>initPrice = []
>>>initPrice.append(1)
>>>initPrice.append(2)
>>>initPrice.pop()
2
>>>initPrice
[1]
However, if you want queue functionality, you can still use lists. You just need to slice them. As juanpa.arrivillaga and Udi were quick to point out, you can pop from the front by passing an index of 0 to the pop
function:
>>>initPrice = []
>>>initPrice.append(1)
>>>initPrice.append(2)
>>>initPrice.pop(0)
1
>>>initPrice
[2]
Upvotes: -1
Reputation: 1369
I would use modulus to reuse the same 4 spots
prices = [0, 0, 0, 0]
i = 0
while True:
prices[i%4] = getNewPrice()
i += 1
# every 20 minutes
if (i % 4 == 0):
# check all same and print
# sleep 5 minutes
Upvotes: 0
Reputation: 96277
Use a Python deque
initialized with the maxlen
parameter:
In [2]: from collections import deque
In [3]: queue = deque(maxlen=4)
In [4]: queue.append(4)
In [5]: queue.append(6)
In [6]: queue.append(5)
In [7]: queue.append(3)
In [8]: queue
Out[8]: deque([4, 6, 5, 3])
In [9]: queue.append(88)
In [10]: queue
Out[10]: deque([6, 5, 3, 88])
Upvotes: 1
Reputation: 504
Python has a Queue class which you could use. Basically you would want to place into the queue whenever you got a new value, but if you ever had 4 or more values, remove a value first. It may look something like this. `
if len(initPrice) >= 4:
initPrice.get()
initPrice.put(someValue)
You would init the queue like this:
initPrice = Queue()
Upvotes: 0