把一段字符串转为list不要用split方法
以下是将字符串转换为列表的另一种方法,不使用split方法:
string = "hello world"
lst = []
# iterate through each character in the string
for char in string:
# if the character is not a space, add it to the list
if char != " ":
lst.append(char)
print(lst) # Output: ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
这种方法将字符串中的每个字符逐个迭代,如果不是空格,则将其添加到列表中。
原文地址: https://www.cveoy.top/t/topic/brC1 著作权归作者所有。请勿转载和采集!