Ask Your Question
0

Iterating over list of tuples

asked 2017-02-24 18:29:42 -0600

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/sage_server.py", 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
    else:
        two_count += 1
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-02-24 21:09:37 -0600

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

Stats

Asked: 2017-02-24 18:29:42 -0600

Seen: 126 times

Last updated: Feb 24