python 字符串转成list
时间: 2023-05-22 09:04:16 浏览: 43
可以使用split()方法将字符串按照指定的分隔符转换成list,例如:
```
string = "hello world"
lst = string.split()
print(lst)
```
输出:
```
['hello', 'world']
```
相关问题
python 把字符串转成list
在Python中,可以使用以下方法将字符串转换为列表:
1. 使用split()函数:split()函数可以将字符串按照指定的分隔符分割成多个子字符串,并返回一个列表。例如,如果要将字符串"apple,banana,orange"转换为列表,可以使用以下代码:
```
string = "apple,banana,orange"
list = string.split(",")
print(list)
```
输出结果为:\['apple', 'banana', 'orange'\]
2. 使用list()函数:list()函数可以将字符串中的每个字符作为列表的一个元素。例如,如果要将字符串"apple"转换为列表,可以使用以下代码:
```
string = "apple"
list = list(string)
print(list)
```
输出结果为:\['a', 'p', 'p', 'l', 'e'\]
请注意,以上方法适用于将字符串转换为一维列表。如果字符串表示的是二维数组,需要根据具体的格式进行解析和转换。
#### 引用[.reference_title]
- *1* *2* [Python基础:str字符串和list列表的转换](https://blog.csdn.net/alice_tl/article/details/79705443)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [python 字符串转换为list](https://blog.csdn.net/xiaoxiang_feizi/article/details/125223067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
python 字符串 转换为list
可以使用split()函数将字符串转换为列表,例如:
str = "hello world"
lst = str.split()
print(lst)
输出结果为:['hello', 'world']