ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 24 Feb 2017 21:09:37 -0600Iterating over list of tupleshttp://ask.sagemath.org/question/36727/iterating-over-list-of-tuples/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 += 1Fri, 24 Feb 2017 18:29:42 -0600http://ask.sagemath.org/question/36727/iterating-over-list-of-tuples/Answer by kcrisman for <p>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:</p>
<p>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 </p>
<p>My code is below:</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/36727/iterating-over-list-of-tuples/?answer=36728#post-id-36728You 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.)Fri, 24 Feb 2017 21:09:37 -0600http://ask.sagemath.org/question/36727/iterating-over-list-of-tuples/?answer=36728#post-id-36728