2017-07-20 178 views
0

我在下面的Python代码错误在Python代码

a=[1,2,3,4,5,6,7,8,9] 
c,d=divmod(len(a),2) 
i=iter(a).next 
print ''.join('%s\t%s\n' % (i(),i()) 
for i in xrange(c))\ 
+ ('%s\t\n' % (i()) if b==1 
    else '') 

我需要打印输出得到错误是

1 2 
3 4 
5 

我得到错误:

Traceback (most recent call last): 
    File "dhsgj.py", line 5, in <module> 
    for i in xrange(c))\ 
    File "dhsgj.py", line 5, in <genexpr> 
    for i in xrange(c))\ 
TypeError: 'int' object is not callable 
+0

你期望'我()'做什么? – user3080953

+0

@ user3080953它调用'next'函数。它在第三行中声明。 –

+0

第5行,您将覆盖第三行的“i” –

回答

0

你做不需要拆分数组,尝试一次迭代两个项目。

我已更新您的代码,使其更容易遵循。这应该工作:

a=[1,2,3,4,5,6,7,8,9] 
iterator = iter(a) 
for first in iterator: 
    try: 
     second = next(iterator) 
    except StopIteration: 
     print first 
    else: 
     print('%s\t%s\n' % (first, second)) 
+0

谢谢你的支持 –