Find the answer to your Linux question:
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 ...
  1. #1
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845

    Python lists

    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 catch if the index is out of bounds.

    The error I'm getting is:

    Code:
        counts(i) = counts(i) + 1
    SyntaxError: can't assign to function call
    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?

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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.

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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
    >>>

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Arg! So simple! And i've just writted a for loop to do it manually

    Code:
    #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
    Well.. Thanks for pointing that out! Much more efficient

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...