首页 > 解决方案 > 在 Python 中用逗号分隔列表中的值

问题描述

我在 python 中有一个列表,我需要在其中使用逗号将一个值分隔为多个值。

以下是我的示例输入列表:

print(lt) #lt is a list

['New film released yesterday\nMovie will be released tomorrow\n
People will watch in theatres\n Movie is very bad\nIt can be good \n\n 
But the director flopped it\n Movie is good']

我在上面的列表中有一个值。如果我在列表中找到,我需要在其中插入逗号(,)以将它们分隔为多个值Movie

预期输出:

 ['New film released yesterday\n','Movie will be released tomorrow\n
    People will watch in theatres\n','Movie is very bad\nIt can be good \n\n 
    But the director flopped it\n ','Movie is good']

在这里,您可以看到具有单个值的列表已使用 拆分为多个值comma (,)。请解释如何解决这个问题。非常感谢!

标签: pythonlist

解决方案


lt=lt[0].replace('Movie', ',Movie').split(',')

这给出了正确的输出。

['New film released yesterday\n','Movie will be released tomorrow\n
    People will watch in theatres\n','Movie is very bad\nIt can be good \n\n 
    But the director flopped it\n ','Movie is good']

推荐阅读