【问题标题】:How to make 'a new list of tuple pairs' from 'a list of tuple pairs' with the most frequent tuples? [closed]如何从具有最频繁元组的“元组对列表”中制作“新的元组对列表”? [关闭]
【发布时间】:2022-01-22 10:38:06
【问题描述】:

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' 是一长串像这样的元组。

【问题讨论】:

  • 请在代码块中格式化您的代码,您可以在代码周围使用 ``` 来做到这一点。
  • 这是代码的一部分。我只是想知道如何做这部分。你怎么知道这是我的作业?
  • 您只提供输入和输出以及对您的功能应该做什么的广泛定义,没有该功能的骨架或草稿,听起来您希望为您完成作业。添加要改进的代码库,然后 SO 将是寻求帮助的正确位置。

标签: python list tuples


【解决方案1】:

我想这就是你想要的

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多