我正在尝试对包含整数和长整数的列表调用 reduce。例如,
reduce( int.__mul__, [ 231212312412L, 3 ], 1 )
但我收到一个未实现的错误。当我用 long 的 mul 方法替换 int 的 mul 方法时,它提示它正在获取一个 int。我必须先将它们全部映射到 long 吗?像这样。
reduce( long.__mul__, map( long, [ 12312412314L, 5 ] ), 1L )
最佳答案
>>> from operator import mul
>>> reduce( mul, map( long, [ 12312412314L, 5 ] ), 1 )
61562061570L
>>> reduce( mul, [ 231212312412L, 3 ], 1 )
693636937236L
>>>
运营商有:
'abs',
'add',
'and_',
'attrgetter',
'concat',
'contains',
'countOf',
'delitem',
'delslice',
'div',
'eq',
'floordiv',
'ge',
'getitem',
'getslice',
'gt',
'iadd',
'iand',
'iconcat',
'idiv',
'ifloordiv',
'ilshift',
'imod',
'imul',
'index',
'indexOf',
'inv',
'invert',
'ior',
'ipow',
'irepeat',
'irshift',
'isCallable',
'isMappingType',
'isNumberType',
'isSequenceType',
'is_',
'is_not',
'isub',
'itemgetter',
'itruediv',
'ixor',
'le',
'lshift',
'lt',
'methodcaller',
'mod',
'mul',
'ne',
'neg',
'not_',
'or_',
'pos',
'pow',
'repeat',
'rshift',
'sequenceIncludes',
'setitem',
'setslice',
'sub',
'truediv',
'truth',
'xor'
关于python - 我想用一个包含 python 中的 longs 和 ints 的列表来调用 reduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7062617/