[英]Convert input String to List after each character without spaces in python
I am trying to ask for an input of an alphanumeric phone number and convert it to a list so that I can convert the alphabet letters to numbers.我试图要求输入字母数字电话号码并将其转换为列表,以便我可以将字母转换为数字。
I have this for the input:我有这个输入:
number=str(input("Enter Alphanumeric phone number:"))
number.split("")
and to try and convert it I tried并尝试转换它我试过
number.split("")
which gave me an error, and这给了我一个错误,并且
list(number)
which just left it as a string.只是把它作为一个字符串。
Any ideas on what is going wrong?关于出了什么问题的任何想法?
One of the issues you have with both approaches is you still have to convert the string to a number in this case int
.这两种方法的问题之一是,在这种情况下,您仍然必须将字符串转换为数字int
。
numbers = str(input('Enter Alphanumeric phone number'))
numbers = list(numbers)
numbers = [int(number) for number in numbers]
for input of 1234567899
you'll get [1,2,3,4,5,6,7,8,9,9]
对于1234567899
输入,您将得到[1,2,3,4,5,6,7,8,9,9]
list(number)
will convert the string to a list of characters. list(number)
将字符串转换为字符列表。 It sounds like you need to assign the result back to number
though, like this:听起来您需要将结果分配回number
,如下所示:
number = list(number)
You can also use list comprehension for one line input.您还可以对一行输入使用列表理解。 In this method you will not need to write more number of lines.在这种方法中,您不需要编写更多行。
list = [int(i) for i in input()]
for input 123456789
you will get [1,2,3,4,5,6,7,8,9]
对于输入123456789
你会得到[1,2,3,4,5,6,7,8,9]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.