1

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

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
andy
  • 525
  • 3
  • 6
  • 22
  • This questions is relatively poorly worded. Are you trying to limit the size of the list to 4, and then empty the list after 20 minutes? – Tyler R Dec 22 '16 at 19:40
  • If `initPrice` is used only to keep track of prices. Why not create it as `set` instead of `list` at the first place? And yes, set is the right choice for this – Moinuddin Quadri Dec 22 '16 at 19:43
  • @Tyler nope, 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. – andy Dec 22 '16 at 19:47

5 Answers5

1

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?

Graham
  • 7,431
  • 18
  • 59
  • 84
Udi
  • 29,222
  • 9
  • 96
  • 129
1

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])
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
0

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()
Tyler R
  • 474
  • 2
  • 6
  • 15
  • 1
    Queue is a complex data structure used for synchronizing (threads etc.). A simple list should be enough for this problem. – Udi Dec 22 '16 at 19:59
  • @Udi but the beauty of python is blackboxing everything and not worrying about things right haha? That's a good point though. Would definitely make sense to whip up a simpler implementation of a queue and utilize that. – Tyler R Dec 22 '16 at 20:26
0

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
chad
  • 1,339
  • 12
  • 10
-1

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]
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76