简体   繁体   English

我如何获得一个数字的个别数字?

[英]How do I get the individual digits of a number?

i want to split up a raw_input into many integers like this 我想将raw_input分成许多这样的整数

TheString = raw_input

>>>12345678

and then spit up TheString to integers 然后将TheString吐出为整数

d1 = 1
d2 = 2
d3 = 3
d4 = 4
d5 = 5
d6 = 6
d7 = 7
d8 = 8

I tried to find it but it did not work what the others suggested. 我试图找到它,但是没有别人建议的那样。

Thank you in advance. 先感谢您。

Don't use different variable names for each digit. 请勿为每个数字使用不同的变量名称。 You can convert the string to a list of integers and index the list to get each digit. 您可以将字符串转换为整数列表,并对列表进行索引以获取每个数字。

>>> s = "314159" # or s = raw_input()
>>> d = map(int, s)
>>> d[0]
3
>>> d[1]
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM