['l-6ren">
gpt4 book ai didi

python - 拆分()不工作?

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:40 27 4
gpt4 key购买 nike

所以我想拆分一个字符串s:

s = "l=2&w=3&h=2"

然而,每当我尝试对 s 使用 split() 函数并将值存储在列表 L 中时,就会出现:

L = s.split()
L --> ['l=2&w=3&h=2']

我做错了什么吗?我如何拆分这个字符串,以便得到:

L = ['l','=','2','&','w','=','3','&','h','=','2']

最佳答案

这实际上比您想象的要容易。

L = list(s)

在 Python 中,字符串是可迭代的,就像列表一样。如果您只需要遍历字符串,您甚至不需要将它存储在列表中。

关于python - 拆分()不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com