## 问题：如何将逗号分隔的字符串转换为Python中的列表？

``mStr = 'A,B,C,D,E' ``

``mList = ['A', 'B', 'C', 'D', 'E']``

Given a string that is a sequence of several values separated by a commma:

``````mStr = 'A,B,C,D,E'
``````

How do I convert the string to a list?

``````mList = ['A', 'B', 'C', 'D', 'E']
``````

## 回答 0

``````>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']``````

``````>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')``````

``````>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']``````

You can use the str.split method.

``````>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']
``````

If you want to convert it to a tuple, just

``````>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')
``````

If you are looking to append to a list, try this:

``````>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']
``````

## 回答 1

``mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]``

``````>>> mStr = "1,A,B,3,4"
>>> mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]
>>> mList
>>> [1,'A','B',3,4]``````

In the case of integers that are included at the string, if you want to avoid casting them to `int` individually you can do:

``````mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]
``````

It is called list comprehension, and it is based on set builder notation.

ex:

``````>>> mStr = "1,A,B,3,4"
>>> mList = [int(e) if e.isdigit() else e for e in mStr.split(',')]
>>> mList
>>> [1,'A','B',3,4]
``````

## 回答 2

``````>>> some_string='A,B,C,D,E'
>>> new_tuple= tuple(some_string.split(','))
>>> new_tuple
('A', 'B', 'C', 'D', 'E')``````
``````>>> some_string='A,B,C,D,E'
>>> new_tuple= tuple(some_string.split(','))
>>> new_tuple
('A', 'B', 'C', 'D', 'E')
``````

## 回答 3

``````def stringtolist(x):
mylist=[]
for i in range(0,len(x),2):
mylist.append(x[i])
return mylist``````

You can use this function to convert comma-delimited single character strings to list-

``````def stringtolist(x):
mylist=[]
for i in range(0,len(x),2):
mylist.append(x[i])
return mylist
``````

## 回答 4

``````#splits string according to delimeters
'''
Let's make a function that can split a string
into list according the given delimeters.
example data: cat;dog:greff,snake/
example delimeters: ,;- /|:
'''
def string_to_splitted_array(data,delimeters):
#result list
res = []
# we will add chars into sub_str until
# reach a delimeter
sub_str = ''
for c in data: #iterate over data char by char
# if we reached a delimeter, we store the result
if c in delimeters:
# avoid empty strings
if len(sub_str)>0:
# looks like a valid string.
res.append(sub_str)
# reset sub_str to start over
sub_str = ''
else:
# c is not a deilmeter. then it is
# part of the string.
sub_str += c
# there may not be delimeter at end of data.
# if sub_str is not empty, we should att it to list.
if len(sub_str)>0:
res.append(sub_str)
# result is in res
return res

# test the function.
delimeters = ',;- /|:'
# read the csv data from console.
csv_string = input('csv string:')
#lets check if working.
splitted_array = string_to_splitted_array(csv_string,delimeters)
print(splitted_array)``````
``````#splits string according to delimeters
'''
Let's make a function that can split a string
into list according the given delimeters.
example data: cat;dog:greff,snake/
example delimeters: ,;- /|:
'''
def string_to_splitted_array(data,delimeters):
#result list
res = []
# we will add chars into sub_str until
# reach a delimeter
sub_str = ''
for c in data: #iterate over data char by char
# if we reached a delimeter, we store the result
if c in delimeters:
# avoid empty strings
if len(sub_str)>0:
# looks like a valid string.
res.append(sub_str)
# reset sub_str to start over
sub_str = ''
else:
# c is not a deilmeter. then it is
# part of the string.
sub_str += c
# there may not be delimeter at end of data.
# if sub_str is not empty, we should att it to list.
if len(sub_str)>0:
res.append(sub_str)
# result is in res
return res

# test the function.
delimeters = ',;- /|:'
# read the csv data from console.
csv_string = input('csv string:')
#lets check if working.
splitted_array = string_to_splitted_array(csv_string,delimeters)
print(splitted_array)
``````

## 回答 5

``````>>> my_string = 'A,B,C,D,E'
>>> my_string.split(",") if my_string else []
['A', 'B', 'C', 'D', 'E']
>>> my_string = ""
>>> my_string.split(",") if my_string else []
[]``````

Consider the following in order to handle the case of an empty string:

``````>>> my_string = 'A,B,C,D,E'
>>> my_string.split(",") if my_string else []
['A', 'B', 'C', 'D', 'E']
>>> my_string = ""
>>> my_string.split(",") if my_string else []
[]
``````

## 回答 6

``````mStr = 'A,B,C,D,E'
list1 = mStr.split(',')
print(list1)``````

`['A', 'B', 'C', 'D', 'E']`

``print(tuple(list1))``

`('A', 'B', 'C', 'D', 'E')`

You can split that string on `,` and directly get a list:

``````mStr = 'A,B,C,D,E'
list1 = mStr.split(',')
print(list1)
``````

Output:

`['A', 'B', 'C', 'D', 'E']`

You can also convert it to an n-tuple:

``````print(tuple(list1))
``````

Output:

`('A', 'B', 'C', 'D', 'E')`