繁体   English   中英

如何从具有最频繁元组的“元组对列表”中创建“新的元组对列表”?

[英]How to make 'a new list of tuple pairs' from 'a list of tuple pairs' with the most frequent tuples?

lst = [('wish', 'NOUN'), ('wish', 'VERB'), ('this', 'DET'), ('this', 'DET'), ('this', 'PROPN'), ('Christmas', 'PROPN')]

word_l = ['this', 'wish', 'Christmas']

程序应该在“lst”中找到“word_l”的单词。 它应该从元组列表“lst”中创建一个新列表,其中包含基于这些单词的最频繁的元组。 new_list 应该包含,

new_list = [('wish', 'NOUN'), ('this', 'DET'), ('Christmas', 'PROPN')]

在“wish”出现一次“NOUN”和“VERB”一次的情况下,选择其中任何一个都可以。 “this”应该只与“DET”一起出现。 'lst' 是一长串像这样的元组。

我想这就是你想要的

lst = [('wish', 'NOUN'), ('wish', 'VERB'), ('this', 'DET'), ('this', 'DET'), ('this', 'PROPN'), ('Christmas', 'PROPN')]
word_l = ['this', 'wish', 'Christmas']
dict = {}
new_l =[]
def main():
    for x in lst:
        if not x in dict:
            dict[x]=1
        else:
            dict[x] += 1
    n=0
    p = ()
    for w in word_l:
        for key in dict:
            if key[0]==w and dict.get(key)>n:
                p = key
                n = dict.get(key)
        
        new_l.append(p)
        n=0
        p=()
    print(new_l)
    
main()

暂无
暂无

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

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