繁体   English   中英

在 Python 中用逗号分隔列表中的值

[英]Separate values in a list by comma in 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 ,我需要在其中插入逗号(,)以将它们分隔为多个值。

预期 output:

 ['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 (,)拆分为多个值。 请解释如何解决这个问题。非常感谢!

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

这给出了正确的 output。

['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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM