如何匹配单词列表和字典项并在python中返回键?

时间:2018-09-09 07:58:43

标签: python list dictionary

topic_dic = {

    'Sports': ['play', 'football', 'cricket', 'ball', 'FIFA', 'Basketball', 'Referee', 'Sachin  Tendulkar', 'messi', 'ronaldo', 'EPL', 'manchester united', 'real Madrid', 'juventus', 'contract', 'paul pogba', 'Barcelona', 'transfer'],

    'Politics': ['Barack Obama', 'Donald Trump', 'Prime Minister', 'PM', 'President', 'BJP', 'Amit Shah', 'Lok Sabha', 'Election', 'Modi', 'Government', 'Supreme Court', 'SC', 'India', 'USA', 'Offical', 'Court', 'Legal', 'Judgement'],

    'Science': ['NASA', 'Space', 'James Webb Space', 'Telescope', 'Alien', 'Dawn', 'Mission', 'Galaxy', 'Habitat', 'Magnet', 'Wave', 'Thermals', 'Temperature', 'Orbit', 'Neutrons', 'Mass', 'Discovery'],

    'Social Entertainment': ['Music', 'Dance', 'Singing', 'Concert', 'Audio', 'Playback', 'Competition', 'Movie', 'Drama', 'Theatre'],

    'General Topics': ['Celebrity', 'Culture', 'Society', 'Business', 'Development', 'Country']
}

topics = ['real','retirement','moment,','messi','pogba','play']

我创建了一个字典,其中包含作为主题的键和与该主题相关的项。我有一个列表,其中包含一些我想与词典项目进行比较的单词。

如何将列表主题的每个元素与字典中的项目进行比较,并返回与列表元素匹配的项的键? 我可以将密钥存储在其他阵列中吗?

2 个答案:

答案 0 :(得分:1)

您可以使用以下dict理解来建立topics中的单词到它们各自类别的映射:

print({w: k for k, l in topic_dic.items() for t in l for w in topics if w in t})

这将返回:

{'play': 'Sports', 'messi': 'Sports', 'real': 'Sports', 'pogba': 'Sports'}

答案 1 :(得分:0)

解决方案

new_array ={}

for i in topics:
    for k in topic_dic:
        if i in str(topic_dic[k]):
            print(f"{i} located in {k}")  
            if k not in new_array:
                new_array[k] = [i] 
            elif k in new_array:
                [v.append(i) for v in new_array.values()]

print(new_array)

更新了解决方案,包括使用找到的keyvalue创建一个新字典,不确定这是否意味着将key存储在新数组中。如果您希望使用输出格式更具体,那将会有所帮助。

输出

(xenial)vash@localhost:~/python/AtBS$ python3.7 loop_info.py 
real located in Sports
messi located in Sports
pogba located in Sports
play located in Sports
{'Sports': ['real', 'messi', 'pogba', 'play']}

这也是一种更易于阅读的字典格式

topic_dic = {
    'Sports': [
        'play','football','cricket','ball','FIFA','Basketball','Referee',
        'Sachin  Tendulkar','messi','ronaldo','EPL','manchester united',
        'real Madrid','juventus','contract','paul pogba','Barcelona','transfer'
    ],
    'Politics': [
        'Barack Obama','Donald Trump','Prime Minister','PM', 'President',
        'BJP','Amit Shah','Lok Sabha','Election','Modi','Government',
        'Supreme Court','SC','India','USA','Offical','Court','Legal',
        'Judgement'
    ],
    'Science': [
        'NASA','Space','James Webb Space','Telescope','Alien','Dawn',
        'Mission','Galaxy','Habitat','Magnet','Wave','Thermals','Temperature',
        'Orbit','Neutrons','Mass','Discovery'
    ],
    'Social Entertainment': [
        'Music','Dance','Singing','Concert','Audio','Playback',
        'Competition','Movie','Drama','Theatre'
    ],
    'General Topics': [
        'Celebrity','Culture','Society','Business','Development','Country'
    ]
}

topics = ['real','retirement','moment,','messi','pogba','play']