python在保留订单的同时分开字符串?

发布于 2025-02-11 06:06:43 字数 614 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猛虎独行 2025-02-18 06:06:43

您可以使用正则表达式为此:

import re

pattern = re.compile(r"(.+:)\s+([^\s].+)")

result = []
for line in lines:
    match = pattern.match(line)
    if match:
        result.append(match.group(1))
        result.append(match.group(2))
    else:
        result.append(line)

You can use regular expressions for that:

import re

pattern = re.compile(r"(.+:)\s+([^\s].+)")

result = []
for line in lines:
    match = pattern.match(line)
    if match:
        result.append(match.group(1))
        result.append(match.group(2))
    else:
        result.append(line)

欢烬 2025-02-18 06:06:43

您可以将嵌套环理解用于输入列表:

l = ['example: Test::YES']

l1 = [j.lower().strip() for i in l for j in i.split(":", 1) if j.strip().lower() != '']
print(l1)

输出:

['example', 'Test::YES']

You can use nested loop comprehension for the input list:

l = ['example: Test::YES']

l1 = [j.lower().strip() for i in l for j in i.split(":", 1) if j.strip().lower() != '']
print(l1)

Output:

['example', 'Test::YES']
御守 2025-02-18 06:06:43

您需要迭代单词列表,对于每个单词,您需要检查是否存在。是否存在。如果存在,则分为2个部分,请并发布部分。将这些预先和帖子附加到最终列表,以及Word中没有在结果列表中添加该单词并跳过该单词

# your code goes here
wordlist = ['example:', 'Test', 'example:         ', 'IGNORE_ME_EXAMPLE']
result = []
for word in wordlist:
    index = -1
    part1, part2 = None, None
    if ':' in word:
        index = word.index(':')
    else:
        result.append(word)
        continue

    part1, part2 = word[:index+1], word[index+1:]
    if part1 is not None and len(part1)>0:
        result.append(part1)
    if part2 is not None and len(part2)>0:
        result.append(part2)
    
print(result)
    

输出的其他操作

['example:', 'Test', 'example:', '         ', 'IGNORE_ME_EXAMPLE']

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 word

# your code goes here
wordlist = ['example:', 'Test', 'example:         ', 'IGNORE_ME_EXAMPLE']
result = []
for word in wordlist:
    index = -1
    part1, part2 = None, None
    if ':' in word:
        index = word.index(':')
    else:
        result.append(word)
        continue

    part1, part2 = word[:index+1], word[index+1:]
    if part1 is not None and len(part1)>0:
        result.append(part1)
    if part2 is not None and len(part2)>0:
        result.append(part2)
    
print(result)
    

output

['example:', 'Test', 'example:', '         ', 'IGNORE_ME_EXAMPLE']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文