Results 1 to 4 of 4
I'm trying to use the following code:
Code:
counts=[]
i = stored.index(t)
counts(i) = counts(i) + 1
Where t is an integer. It is wrapped in a try block to ...
- 08-30-2007 #1
Python lists
I'm trying to use the following code:
Where t is an integer. It is wrapped in a try block to catch if the index is out of bounds.Code:counts=[] i = stored.index(t) counts(i) = counts(i) + 1
The error I'm getting is:
I know that the error is occurring because I'm using a variable to point to the index in counts but is there a work-around for it?Code:counts(i) = counts(i) + 1 SyntaxError: can't assign to function call
- 08-30-2007 #2
From the block you've shown (As far as I can tell), counts is an array, not a function. That's why you're getting a syntax error.
- 08-30-2007 #3
In case I wasn't clear enough (I have that problem sometimes):
Code:>>> counts = [ 1, 2, 3, 7, 9, 10 ] >>> t = 4 >>> counts[t] = counts[t] + 1 >>> print counts[t] 10 >>>
- 08-30-2007 #4
Arg! So simple! And i've just writted a for loop to do it manually

Well.. Thanks for pointing that out! Much more efficientCode:#clear tmplist tmplist = [] #loop through each index of counts for j in range(len(counts)) : #check if current index = this time if(j==i) : #increment time count tmplist.append(counts[j]+1) else : #no match - just copy previous count tmplist.append(counts[j]) #copy tmplist into counts counts = tmplist


Reply With Quote