-1

I have a list with fixed size of 10 which i will put CPU percent of my computer with 2 seconds of interval. What i'm trying to do is, removing 1st element shifting each element into previous index of list recording 11th value at last index of list

Below you may see my code. I did some debugging but i'm confused right now. How can i fix this?

__author__ = 'tim'
#-*- coding: utf-8 -*-
import psutil, os, time

def getCpuRate():
   myList = [None]*10
   myString=" "
   myString2 = " "
   i = 0
   j = 0

   while True:
      if myList[9] is None:
         myList[i] = psutil.cpu_percent(interval=2)
         myString = myString + (str(myList[i]) + " ")
         i = i+1
         print i , myString
         #time.sleep(3)
      else:
         while i>0:
            myList[j] = myList[j+1]
            #print myList[j+1] , myList[j]
            for k in range(len(myList)):
               myString2 = myString + (str(myList[k]) + "")
            print i , j , myString2
            j = j+1
            i = i-1
            if j >= 9:
               myList[j] = psutil.cpu_percent(interval=2)
               print i , j , myString2
               j -= 1


print "a"
getCpuRate()
'''
mySecondList = getCpuRate()
for x in range(len(mySecondList)):
   print mySecondList[x]
'''
print "b"
t1w
  • 1,408
  • 5
  • 25
  • 55
  • if you only have 10 elements where does the 11th fit? – Padraic Cunningham Feb 13 '15 at 09:45
  • i'm trying to overwrite 2nd to 1st, then 3rd to 2nd and so on. Once 10th element is copied over 9th, i need 11th to be written over 10th – t1w Feb 13 '15 at 09:46
  • 2
    is this homework or can you simply use a deque? `from collections import deque deq = deque(maxlen=10)` – Padraic Cunningham Feb 13 '15 at 09:49
  • it's not a hw and i can use deque but i simply prefer not to :) – t1w Feb 13 '15 at 09:58
  • 1
    what about `if len(lst) > 10`remove first item? – Padraic Cunningham Feb 13 '15 at 09:59
  • I dont' get why you don't want to use a deque, because lists are not the best choice for what you have in mind, see documentation: "Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation." – SmCaterpillar Feb 13 '15 at 10:02
  • I agree with @SmCaterpillar. The deque is not only the adequate data structure but it also results in a very compact function. I see no reason not to use it. – igordsm Feb 13 '15 at 10:08

2 Answers2

0

Why don't you use a deque?

Just fill in 10 dummy values and then use append and pop. So everything gets shifted to the left. Or you specify the maxlen=10 (maximum length) of the deque and only use append.

SmCaterpillar
  • 6,683
  • 7
  • 42
  • 70
0

Instead of using a list you could use a deque (documentation link), which is a list optimized for the kind of operations you want.

import collections
q = collections.deque()

# add element at the end:
q.append('cpu percentage here')
# add element at the beginning:
q.appendleft('cpu percentage here')

# remove first element:
q.popleft()
# remove last element:
q.pop()

Then, you loop becomes much simpler:

import psutil
import collections

def getCpuRate():
    q = collections.deque(maxlen=10)
    while True:
        q.appendleft(psutil.cpu_percent(interval=2))
        print(q)

Notice that I the ordering so that we insert the new values at the beginning and remove the old ones at the end. This way the new values come first when printing q. If you don't want that, you can change appendleft by appendand pop by popleft.

igordsm
  • 464
  • 2
  • 8