python在保留订单的同时分开字符串?
我在Python中有一系列的列表,我需要保留顺序并分开一些字符串。
拆分字符串的条件是,在的第一次匹配之后:
有一个无空间/新行/Tab char。
例如,必须将其分开:
示例:test
to ['example':,'test']
时保持相同:示例:
,ignore_me_example
给定的输入: ['example:test','示例:','ignore_me_example']
我期待: ['example:','test','示例:','ignore_me_example']
请注意,拆分字符串彼此相处并遵循原始顺序。
另外,每当我拆分字符串时,我都不想再次检查拆分零件。换句话说,我不想在分开后检查'test'
。
为了更清楚,给定这样的输入:
['示例:test :: yes']
我期望: ['example:','test :: yes']
I have a list of strings in python, where I need to preserve order and split some strings.
The condition to split a string is that after first match of :
there is a none space/new line/tab char.
For example, this must be split:
example: Test
to ['example':, 'Test']
While this stays the same: example:
, IGNORE_ME_EXAMPLE
Given an input like this:['example: Test', 'example: ', 'IGNORE_ME_EXAMPLE']
I'm expecting:['example:', 'Test', 'example: ', 'IGNORE_ME_EXAMPLE']
Please Note that split strings are yet stick to each other and follow original order.
Plus, whenever I split a string I don't want to check split parts again. In other words, I don't want to check 'Test'
after I split it.
To make it more clear, Given an input like this:
['example: Test::YES']
I'm expecting:['example:', 'Test::YES']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用正则表达式为此:
You can use regular expressions for that:
您可以将嵌套环理解用于输入列表:
输出:
You can use nested loop comprehension for the input list:
Output:
您需要迭代单词列表,对于每个单词,您需要检查
是否存在。
是否存在。如果存在,则分为2个部分,请:
并发布部分。将这些预先和帖子附加到最终列表,以及Word中没有:
在结果列表中添加该单词并跳过该单词输出的其他操作
you need to iterate over your list of words, for each word, you need to check if
:
present or not. if present the then split the word in 2 parts, pre:
and post part. append these pre and post to final list and if there is no:
in word add that word in the result list and skip other operation for that wordoutput