python代码中的错误

时间:2017-07-20 04:27:01

标签: python python-2.7

我在下面的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

1 个答案:

答案 0 :(得分: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))