我使用此功能来检查号码:
def numValid(code):
LOOKUP = (0, 2, 4, 6, 8, 1, 3, 5, 7, 9)
code = reduce(str.__add__, filter(str.isdigit, code))
evens = sum(int(i) for i in code[-1::-2])
odds = sum(LOOKUP[int(i)] for i in code[-2::-2])
return ((evens + odds) % 10 == 0)
我用numValid(242344)
where 242344
is来称呼它dtype: int64
。
为什么我收到错误:
TypeError: 'int' object is not iterable on the line:
filter(str.isdigit, code))
整数不是序列——它们只是原子的东西,所以你不能迭代它们。看起来您正在尝试将字符串函数应用于每个数字 - 您需要转换code
为字符串(即str(code)
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句