【问题标题】:Python 3.3.0 -- unsupported operand type(s) for +: 'int' and 'list'Python 3.3.0 -- + 不支持的操作数类型:'int' 和 'list'
【发布时间】:2017-01-30 11:14:59
【问题描述】:

这是我的功能

def f(*a,b):
    print (sum(a) , b)

以下调用导致错误

f([x for x in range(100)] , b=0)

Traceback (most recent call last):
  File "<pyshell#61>", line 1, in <module>
    f([x for x in range(100)] , b=0)
  File "<pyshell#59>", line 2, in f
    print (sum(a) , b)
TypeError: unsupported operand type(s) for +: 'int' and 'list'

当我不使用 sum(a) 而只使用 a 时,这工作正常,请告诉我这是怎么回事。

【问题讨论】:

    标签: python-3.x arguments args packing


    【解决方案1】:

    您需要解压列表才能将列表传递到a

    f(*[x for x in range(100)], b=0)
    # ^
    

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多