简体   繁体   English

列出 Python 中两个列表中不匹配的单词

[英]List mismatched words from two lists in Python

I want to print the words that are not present in either of the sentences and if one word is present twice in the first sentence but only once in the second sentence, I want to print that word too.我想打印两个句子中都不存在的单词,如果一个单词在第一个句子中出现两次但在第二个句子中只出现一次,我也想打印那个单词。 For example:例如:

a = "The winter winter season is cold"
b = "The summer winter season is hot"

Output: {'winter','cold','summer','hot'} Output: {'winter','cold','summer','hot'}

I tried to use Set in python but it gives me this output: {'hot', 'cold', 'summer'}我尝试在 python 中使用 Set 但它给了我这个 output: {'hot', 'cold', 'summer'}

def uncommonwords(a,b):
    listA = a.split()
    listB = b.split()
    listC = listA +listB
    return set(listC) - set(listA).intersection(listB)
print(uncommonwords(a,b))

Whats happening is:发生的事情是:

  1. you converting list to set, which leads to dropping of duplicate words.您将列表转换为集合,这会导致删除重复的单词。 ( winter is occuring only once in listA) winter在listA中只出现一次)
  2. As winter is present in one set and not another, its not being displayed.由于冬天出现在一组而不是另一组中,因此它没有被显示。

You need the additional list of words which are present twice in a and once in b .您需要额外的单词列表,这些单词在a中出现两次,在b中出现一次。 When you work with sets you lose the count of words in the sentence.当您使用集合时,您会丢失句子中的单词数。 Hence, you can use a dict instead:因此,您可以使用 dict 代替:

dictA = {x:a.count(x) for x in a.split()}
dictB = {x:b.count(x) for x in b.split()}

[x for x in dictA.keys() if dictA[x]==2 and dictB[x]==1] 

Output Output

['winter']

You can compress the whole function into您可以将整个 function 压缩成

[x for x in dictA.keys() if (x not in dictB.keys()) or (dictA[x]==2 and dictB[x]==1)] + [x for x in dictB.keys() if (x not in dictA.keys())]

Output Output

['winter', 'cold', 'summer', 'hot']

Simply use 'collections' package只需使用“集合”package

from collections import Counter
a = "The winter winter season is cold" 
b = "The summer winter season is hot"

def uncommonwords(a,b):
  listA = Counter(a.split())
  listB = Counter(b.split())

  return list((listA - listB) + (listB - listA))

print(uncommonwords(a,b))

And your output will be你的 output 将是

['winter', 'cold', 'summer', 'hot']

in this case, we are using the counter of each word and manipulate difference of both list and getting result在这种情况下,我们使用每个单词的计数器并操纵列表和获取结果的差异

you could try this too.你也可以试试这个。

a = "The winter winter season is cold"
b = "The summer winter season is hot"


def uncommonwords(a, b):
    a = a.split()
    b = b.split()
    h = set([x for x in a if a.count(x) > 1] + [x for x in b if b.count(x) > 1])
    k = set(a).symmetric_difference(set(b))
    return set.union(k, h)


print(uncommonwords(a, b))

or if you have iteration_utilities instead of set([x for x in a if a.count(x) > 1] + [x for x in b if b.count(x) you can do set(list(duplicates(a)) + list(duplicates(b))) too或者如果你有iteration_utilities实用程序而不是set([x for x in a if a.count(x) > 1] + [x for x in b if b.count(x)你可以set(list(duplicates(a)) + list(duplicates(b)))

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

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