python - 我想用一个包含 python 中的 longs 和 ints 的列表来调用 reduce

标签 python int long-integer reduce

我正在尝试对包含整数和长整数的列表调用 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/

相关文章:

python - 在 centOS 7 中安装 pip

python - 当我运行代码时,他们告诉我 "int"对象在第 4 行不可调用

c# - 整数到整数数组 C#

c - 如何编辑文件中的特定值?

c - 我在程序 C 中遇到了 Time Limit Exceeded 错误。我该如何克服它?

c - 如何在 C 中将 char 数组转换为变量 long?

python - Twisted Python 客户端/服务器的 Kerberos 身份验证

c++ - 将 UINT 转换为 WS_STRING

c - 此操作的 unsigned long int 是否正确?

python - 如何在 Pandas 列中获取唯一的子串