如何在python中拆分列表?

-5 python split list

我有一些列表如下:

['apple,orange,cherry', 'tomato,potato,cucumber', 'pear,grape, kiwi']
['fish,chicken,beef', 'milk,juice,tea', 'Facebook,twitter,instagram']
...
Run Code Online (Sandbox Code Playgroud)

我想在列表中拆分字符串,如下所示:

[['apple', 'orange', 'cherry'], [...], [...]]
...
Run Code Online (Sandbox Code Playgroud)

我试过了split,但没用.

Bre*_*bel 6

你可能只需要传递一个分裂字符(即,)split.默认情况下,它仅在空格上分割.

a = ['apple,orange,cherry', 'tomato,potato,cucumber', 'pear,grape, kiwi']
b = [s.split(',') for s in a]
Run Code Online (Sandbox Code Playgroud)

  • @JQ我不确定你的意思.你的字符串是用逗号开头还是结尾?或者列表中是否有空字符串?您的字符串必须与您提供的示例不同. (2认同)