简体   繁体   English

Python 比较两个列表,如果单词匹配添加到另一个列表

[英]Python compare two lists, if words match add to another list

Im trying to compare 'words' with 'if_contains', when they match they should be added into save list.我试图将“单词”与“if_contains”进行比较,当它们匹配时,它们应该被添加到保存列表中。 Expected output - ['one', 'two eight nine'].预期 output - ['一','二八九']。 The output im getting - ['one'] output 我得到 - ['one']

if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']

for word in words:
    if word in if_contains:
        save.append(words)

print(save)

The first thing is that you save the whole words list instead of single elements.第一件事是您保存整个单词列表而不是单个元素。 You probably wanted to do你可能想做

save.append(word)

instead of代替

save.append(words)

Secondly, if you want to save two eight nine as matching with the if_contains list, then instead of performing if word in if_contains , you should ask whether for any element of if_contains , this element is in word (which isn't the best choice for the variable name as it represents a few words occasionally).其次,如果你想保存two eight nine作为匹配if_contains列表,那么不要执行if word in if_contains ,你应该询问if_contains的任何元素是否在word中(这不是最好的选择变量名,因为它偶尔代表几个词)。 Final solution:最终解决方案:

if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']

for word in words:
    for el in if_contains:
        if el in word:
            save.append(word)

print(save)

You're appending the whole list to the save list variable.您将整个列表附加到save列表变量。 Use save.append(word) instead of save.append(words)使用save.append(word)而不是save.append(words)

if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']

for word in words:
    if word in if_contains:
        save.append(word)

print(save)

Please take a look.请看一下。 There was mistake with words and you need to use nested loop with that.单词有错误,您需要使用嵌套循环。

if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']

for if_c in if_contains:
    for word in words:
        if if_c in word:
            save.append(word)

print(save)

Use list comprehension使用list comprehension

if_contains = ['one', 'two', 'three']
save = []
words = ['one', 'five', 'six', 'two eight nine']

[word for word in words if word in if_contains]
['one']

Ehy, but in case I want to get just the whole-word and not a substring?呃,但如果我只想得到整个单词而不是 substring?

if_contains = ['napoli', 'salvini', 'one']
save = []
words = ['napoli', 'pierosalvini', 'stellone', 'pietrone']

for word in words:
    for el in if_contains:
        if el in word:
            save.append(word)

print(save)

In this case I've all the wods with the substrings in them.在这种情况下,我拥有所有带有子字符串的单词。 But I'd like to have just napoli, because it's 100% matched.但我想要那不勒斯,因为它是 100% 匹配的。 All the characters have to be the same.所有字符必须相同。

Thanks谢谢

Trying to solve:试图解决:

p_calcio01=open('parole_calcio.txt')
p_calcio = [line.rstrip('\n') for line in p_calcio01]

for row in frame:
    for word in p_calcio:
        for word1 in frame["Cleaned Text into list"]: #words list in a dataframe
            if word1 in word:
                 frame["Cluster"] = "Calcio"
frame

The parole_calcio.txt is a simple word(s) list every row. parole_calcio.txt 是每行的简单单词列表。 Dataframe is: enter image description here Dataframe 是:在此处输入图像描述

frame["Cluster"] is a new Column frame["Cluster"]是一个新列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM