在python列表中拆分带逗号的字符串
我有这个清单在python列表中拆分带逗号的字符串,python,list,split,Python,List,Split,我有这个清单 l = [[hello,world],[i need help, python]] l [0] = [hello, world] l [1] = [i need help, python] 我需要用逗号拆分l[0]和l[1],但我不知道怎么做 因为“你好,世界”和“我需要帮助,python”是字符串 有人能帮我吗 只需在列表上循环,并使用myString.split(“,”)拆分每个条目,我想您需要在列表上循环: >>> l = [['hello,world
l = [[hello,world],[i need help, python]]
l [0] = [hello, world]
l [1] = [i need help, python]
我需要用逗号拆分l[0]
和l[1]
,但我不知道怎么做
因为“你好,世界”
和“我需要帮助,python”
是字符串
有人能帮我吗 只需在列表上循环,并使用myString.split(“,”)
拆分每个条目,我想您需要在列表上循环:
>>> l = [['hello,world'],['i need help, python']]
>>> for x in l:
... print x[0] # if you sung python 3x print(x[0])
...
hello,world
i need help, python
如果要拆分字符串:
>>> for x in l:
... print x[0].split(',')
...
['hello', 'world']
['i need help', ' python']
阅读这里有一个递归方法,我用它来解决这样的问题
def explode(sep,x):
if isinstance(x,(str,unicode)): return x.split(sep)
elif isinstance(x,list): return [explode(sep,each) for each in x]
elif isinstance(x,dict):return {key:explode(sep,x[key]) for key in x}
else: raise ValueError("function doesn't support",type(x))
l = [["hello,world"],["i need help, python"]]
l = explode(",",l)
它们目前不是字符串。你能至少把报价包括进去吗?预期的产出是什么?然后呢?OP没有确切地指定拆分结果应该放在哪里。他在拆分时遇到问题,我想他知道如何处理之后的结果。然后他可以添加拆分x[0]。拆分()然后呢?是否应更改原始列表?存储在新的平面列表中?缺少太多细节。为什么有人投了反对票???我投了,因为你最初的编辑没有解决这个问题。OP不清楚,所以我涵盖了所有方面:)