0

我有一个字符串。现在,如果两个不同列表中的任何内容匹配,我想将字符串拆分为多个部分。我怎样才能做到这一点 ?我有什么。

dummy_word = "I have a HTML file"
dummy_type = ["HTML","JSON","XML"]
dummy_file_type = ["file","document","paper"]

for e in dummy_type:
    if e in dummy_word:
        type_found = e
        print("type ->" , e)
        dum = dummy_word.split(e)
        complete_dum = "".join(dum)

        for c in dummy_file_type:
            if c in complete_dum:
                then = complete_dum.split("c")
                print("file type ->",then)

在给定的场景中,我的预期输出是["I have a", "HTML","file"]

4

3 回答 3

1

这对我有用:

dummy_word = "I have a HTML file"
dummy_type = ["HTML","JSON","XML"]
dummy_file_type = ["file","document","paper"]

temp = ""
dummy_list = []
for word in dummy_word.split():
    if word in dummy_type or word in dummy_file_type:
        if temp:
            dummy_list.append(temp)
            print(temp, "delete")

        print(temp)
        new_word = word + " "
        dummy_list.append(new_word)
        temp = ""
    else:
        temp += word + " "
    print(temp)
print(dummy_list)
于 2020-04-20T04:53:53.727 回答
1

这类任务由itertools.groupby(). 如果单词在单词集中,或者False不在单词集中,这里的键将转换为单个单词。这允许所有非特殊词组合在一起,每个特殊词成为自己的元素:

from itertools import groupby

dummy_word = "I have a HTML file"
dummy_type = ["HTML","JSON","XML"]
dummy_file_type = ["file","document","paper"]

words = set(dummy_type).union(dummy_file_type)

[" ".join(g) for k, g in 
 groupby(dummy_word.split(), key=lambda word: (word in words) and word)]

# ['I have a', 'HTML', 'file']
于 2020-04-20T04:57:40.467 回答
1

另一种使用方法re

>>> list(map(str.strip, re.sub("|".join(dummy_type + dummy_file_type), lambda x: "," + x.group(), dummy_word).split(',')))
['I have a', 'HTML', 'file']
>>> 

首先,通过使用 连接所有类型来形成一个正则表达式模式join。使用re.sub,字符串被替换,其中标记以逗号开头,然后我们使用逗号分隔符拆分字符串。map用于去除空格。

于 2020-04-20T05:24:46.763 回答