我有一个这样的映射关键字。你知道吗
categories_mapping = {
'comics': 'Comic Books',
'cartoons': 'Comic Books',
'manga': 'Comic Books',
'video and computer games': 'Video Games',
'role playing games': 'Video Games',
'immigration': 'Immigration',
'police': 'Police',
'environmental': 'Environment',
'celebrity fan and gossip': 'Celebrity',
'space and technology': 'NASA / Space',
'movies and tv': 'TV and Movies',
'elections': 'Elections',
'referendums': 'Elections',
'sex': 'Sex',
'music': 'Music',
'technology and computing': 'Technology'}
还有这样的名单。你知道吗
labels = ['technology and computing', 'arts and technology']
如果列表中的任何单词在字典的关键字中,我想返回字典的值。你知道吗
这是我想到的,但我认为这不是很Python。你知道吗
cats = []
for k,v in categories_mapping.items():
for l in labels:
if k in l:
cats.append(v)
return cats
我想要的结果是['Technology']
有更好的方法吗?你知道吗
您可以使用
intersection
标签和字典键:部分匹配的更新:
相关问题 更多 >
编程相关推荐