简体   繁体   English

在python中没有空格的每个字符后将输入字符串转换为列表

[英]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.

相关问题 如何在Python中将字符串的每个字符转换为list元素的各个元素? - How to convert each character of a string into individual elements of list element in Python? python:将每个元素转换为列表中的字符串,不使用 for 循环 - python: convert each element to string in a list, without using for loop 如何将带空格的字符串转换为在python中列出 - How to convert a string with spaces to list in python 如何使用Python转换字符串中的每个字符 - How to Convert Each Character in a String using Python 如何将具有多个字符的字符串转换为列表,并且每个字符都是 python 中的字符串 [暂停] - How to convert a string with many characters to a list and that each character is a string in python [on hold] 如何将列表转换为 Python 中带空格的字符串? - How do I convert a list into a string with spaces in Python? 在 Python 中将每个列表元素转换为字符串 - Convert each list element to string in Python 在字符串列表中,对于每个字符串,在特定字符之后删除字符串的一部分 - In a list of strings, for each string remove part of a string after a specific character python 2.7.5+打印列表,逗号后无空格 - python 2.7.5+ print list without spaces after the commas 用空格分割一串整数而不转换为python中的列表 - Splitting a string of integers with spaces without converting to a list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM