python - 在 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 (,)
。请解释如何解决这个问题。非常感谢!
解决方案
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']
推荐阅读
- java - 删除按下后退按钮上的重复项目
- html - 我的视频列表来自 json 文件。onended 功能使其转到下一个视频
- java - Maven Release Plugin - 指定要在嵌套 Maven 调用中使用的 Java Home
- javascript - 如何获取 veracode 漏洞报告的详细信息?
- python - 打印字典条目的索引号,加上它的键,加上它的值
- css - 我的 css 在 node.js 中不起作用 收到此错误(描述中的错误),因为它太长了
- python - 在 python 中使用可选的 **kwargs
- asp.net-core - 如何在 NLog 中禁用 Hangfire 日志记录?
- c# - 我新下载 dotnet 和 C# 时出现的一些问题
- java - JavaBeans 是否不再需要可序列化?