[英]I am trying to collect 10 numbers as input, store them in a list, then display the max, min, total, and average of the numbers
[英]I am trying to separate digits of a number and then store them in a list
我试图将数字分开,然后将它们存储在列表中。 有人可以解释一下“数字”行的工作原理吗?
码:
n = [153,423,1,22]
for i in range(0, len(n)):
digits = [int(d) for d in str(n[i])]
学习语言的一部分是学习如何进行诊断。 在这种情况下,要弄清楚正在发生的情况,您应该“展开”列表推导中的循环。 然后在循环进行时打印值。
n = [153,423,1,22]
for i in range(0, len(n)):
# digits = [int(d) for d in str(n[i])]
num = n[i]
num_str = str(num)
print ("Working on number", num_str)
digits = []
for d in num_str:
digits.append(int(d))
print ("Add digit", d, digits)
这会给您一些有用的信息吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.