Ask Your Question

Iterating over list of tuples

asked 2017-02-25 01:29:42 +0100

collegesista gravatar image

I am attempting to index into the y-coordinates of each tuple to count how many tuples have a y-coordinate of 0, 1, or 2 but am receiving an error message which looks like this:

Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/", line 982, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> TypeError: 'sage.rings.integer.Integer' object is not iterable

My code is below:

zero_count, one_count, two_count = 0

for i in range(len([list])):
    if list[i][1] == 0:
        zero_count += 1
    elif list[i][1] == 1:
        one_count += 1
        two_count += 1
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-02-25 04:09:37 +0100

kcrisman gravatar image

You may have other issues, but in Python you can't assign things this way as in your first line. Try

zero_count, one_count, two_count = 0, 0, 0

instead. (Technical explanation is that commas create tuples, so you are assigning the output of a tuple to those variable names.)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-02-25 01:29:42 +0100

Seen: 822 times

Last updated: Feb 25 '17