Python:求一个数的位数的幂次方的各位数和等于该数本身的数

Python: Find the numbers for which the sum of the digits raised by the power of the number of digits in a number is equal to the number itself

Find the numbers for which the sum of the digits raised by the power of the number of digits in a number is equal to the number itself.

例如

 2^1 = 2
 1^3 + 5^3 + 3^3 = 153
 3^3 + 7^3 + 0^3 = 370
 1^4 + 6^4 + 3^4 + 4^4 = 1634

编写了以下代码:

armstrong_num=[]
for i in range(0, 10000):
    n=len(i)
    total=0
    for j in i:
        total = total+(j)**n
        if total==int(i):
            armstrong_num.append()
print(armstrong_num)
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '153', '370', '370', '371', '407', '1634', '6688', '8208', '9474']

除一个问题外,一切正常。数字 370 被识别了两次。任何更正代码的提示。

下面的代码说明了您 post 上留下的一些评论。 ** 更新了对@Mark 评论的解释 post 编辑如下。

armstrong_num=[]
for i in range(0, 10000):
    n=len(str(i))
    total=0
    for char in str(i):
        total = total+(int(char))**n
    if total==int(i):
       armstrong_num.append(i)
print(armstrong_num)