python - 字符串列表中字符串出现的双重列表理解

标签 python python-3.x list split list-comprehension

我有两个列表列表:

text = [['hello this is me'], ['oh you know u']]
phrases = [['this is', 'u'], ['oh you', 'me']]
我需要拆分文本,使短语中出现的单词组合成为单个字符串:
result = [['hello', 'this is', 'me'], ['oh you', 'know', 'u']
我尝试使用 zip() 但它连续遍历列表,而我需要检查每个列表。我还尝试了 find() 方法,但从这个例子中它也会找到所有字母 'u' 并将它们变成一个字符串(就像在单词 'you' 中它使它变成 'yo', 'u')。
我希望 replace() 在用列表替换字符串时也能工作,因为它可以让我做类似的事情:
for line in text:
        line = line.replace('this is', ['this is'])
但是尝试了所有方法,在这种情况下,我仍然没有找到任何适合我的方法。你能帮我解决这个问题吗?

最佳答案

用原始海报澄清:
鉴于文本pack my box with five dozen liquor jugs和短语 five dozen结果应该是:

['pack', 'my', 'box', 'with', 'five dozen', 'liquor', 'jugs']
不是:
['pack my box with', 'five dozen', 'liquor jugs']
每个文本和短语都转换为 Python 单词列表 ['this', 'is', 'an', 'example']这可以防止在单词内匹配 'u'。
文本所有可能的子短语由 compile_subphrases() 生成.
较长的短语(更多的词)首先生成,因此它们在较短的短语之前匹配。 'five dozen jugs'将始终优先匹配 'five dozen''five' .
短语和子短语使用列表切片进行比较,大致如下:
    text = ['five', 'dozen', 'liquor', 'jugs']
    phrase = ['liquor', 'jugs']
    if text[2:3] == phrase:
        print('matched')
使用这种比较短语的方法,脚本遍历原始文本,用挑选出的短语重写它。
texts = [['hello this is me'], ['oh you know u']]
phrases_to_match = [['this is', 'u'], ['oh you', 'me']]
from itertools import chain

def flatten(list_of_lists):
    return list(chain(*list_of_lists))

def compile_subphrases(text, minwords=1, include_self=True):
    words = text.split()
    text_length = len(words)
    max_phrase_length = text_length if include_self else text_length - 1
    # NOTE: longest phrases first
    for phrase_length in range(max_phrase_length + 1, minwords - 1, -1):
        n_length_phrases = (' '.join(words[r:r + phrase_length])
                            for r in range(text_length - phrase_length + 1))
        yield from n_length_phrases
        
def match_sublist(mainlist, sublist, i):
    if i + len(sublist) > len(mainlist):
        return False
    return sublist == mainlist[i:i + len(sublist)]

phrases_to_match = list(flatten(phrases_to_match))
texts = list(flatten(texts))
results = []
for raw_text in texts:
    print(f"Raw text: '{raw_text}'")
    matched_phrases = [
        subphrase.split()
        for subphrase
        in compile_subphrases(raw_text)
        if subphrase in phrases_to_match
    ]
    phrasal_text = []
    index = 0
    text_words = raw_text.split()
    while index < len(text_words):
        for matched_phrase in matched_phrases:
            if match_sublist(text_words, matched_phrase, index):
                phrasal_text.append(' '.join(matched_phrase))
                index += len(matched_phrase)
                break
        else:
            phrasal_text.append(text_words[index])
            index += 1
    results.append(phrasal_text)
print(f'Phrases to match: {phrases_to_match}')
print(f"Results: {results}")
结果:
$python3 main.py
Raw text: 'hello this is me'
Raw text: 'oh you know u'
Phrases to match: ['this is', 'u', 'oh you', 'me']
Results: [['hello', 'this is', 'me'], ['oh you', 'know', 'u']]
要使用更大的数据集测试此答案和其他答案,请在代码开头尝试此操作。它在单个长句子上生成 100 多个变体,以模拟 100 多个文本。
from itertools import chain, combinations
import random

#texts = [['hello this is me'], ['oh you know u']]
theme = ' '.join([
    'pack my box with five dozen liquor jugs said',
    'the quick brown fox as he jumped over the lazy dog'
])
variations = list([
    ' '.join(combination)
    for combination
    in combinations(theme.split(), 5)
])
texts = random.choices(variations, k=500)
#phrases_to_match = [['this is', 'u'], ['oh you', 'me']]
phrases_to_match = [
    ['pack my box', 'quick brown', 'the quick', 'brown fox'],
    ['jumped over', 'lazy dog'],
    ['five dozen', 'liquor', 'jugs']
]

关于python - 字符串列表中字符串出现的双重列表理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65866679/

相关文章:

java - 将两个列表合并为一个列表

python - Databricks Autoloader - 列转换 - 列不可迭代

python - 将自定义训练的 NER 模型与斯坦福 CoreNLP 中的现有默认模型集成

python - 将测试标记为在独立进程中运行

python - 在 python、shell 和 stdout 中使用 ffmpeg

python - 如何在字典中生成值的有序线性组合?

python - 排序列表时嵌套的 lambda 语句

python - 为什么我在 python 3.6 的 jupyter Notebook 中收到警告以及如何修复它们?

python - 引用一个显示 'local variable ' Final_ans' 在声明之前引用的变量'

python - 在 Python 中替换字符串中的特殊字符