我只想计算字符串中某些元素(其索引值+ 1在 N 中)的乘积。
这很好用:
start = 1
end = 1000000
N = (1, 10, 100, 1000, 10000, 100000, 1000000)
product = 1
concatenated_numbers_str = ''.join([str(x) for x in range(1, end + 1)])
for n in N:
product *= int(concatenated_numbers_str[n - 1])
print(product)
但有什么更好的方法呢?
谢谢
答案 0 :(得分:0)
我喜欢Pavel的答案,但它应该是n-1而不是n:
import operator
end = 1000000
N = (1, 10, 100, 1000, 10000, 100000, 1000000)
concatenated_numbers_str = ''.join([str(x) for x in range(1, end + 1)])
print reduce(operator.mul, [int(concatenated_numbers_str[n-1]) for n in N], 1)