【问题标题】:Pythonic way to check if element in a list contains in key dictionary检查列表中的元素是否包含在键字典中的 Pythonic 方法
【发布时间】:2015-10-27 16:35:31
【问题描述】:

我有一个这样的映射关键字。

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']

如果列表中的任何单词在字典的键中,我想返回字典的值。

这是我想出的,但我认为这不是很pythonic。

cats = []
for k,v in categories_mapping.items():
    for l in labels:
        if k in l:
            cats.append(v)
return cats

我想要的结果是['Technology']

有更好的方法吗?

【问题讨论】:

  • 对不起,这里的labels 是什么?
  • 你能显示你的预期输出吗?
  • 您可能需要考虑使用frozensets 作为您的字典键。它将使查找速度更快,并更好地表示数据。
  • 你的意思是for l in labels: if k in l 还是你的意思是if k in labels

标签: python dictionary


【解决方案1】:

您可以使用intersection 的标签和字典键:

cats = [categories_mapping[key] for key in set(labels).intersection(categories_mapping)]

部分匹配更新:

cats = [categories_mapping[key] for key in categories_mapping if any(label.lower() in key.lower() for label in labels)]

【讨论】:

  • 抱歉,我刚刚更新了我的答案,这适用于部分匹配吗?
【解决方案2】:
>>> [categories_mapping[l] for l in labels if l in categories_mapping]
['Technology']

【讨论】:

    【解决方案3】:
    >>> 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']
    >>> cats = []
    >>> for l in labels:
        if l in categories_mapping:
            cats.append(categories_mapping[l])
    >>> cats
    ['Technology']
    

    【讨论】:

      【解决方案4】:
      cats = [v for k, v in categories_mapping.items() if k in labels]
      

      【讨论】:

      • 如果您为您的解决方案提供解释,那就太好了。
      【解决方案5】:

      你可以摆脱外循环:

      for l in labels:
          if l in categories_mapping:
              cats.append(categories_mapping[l])
      

      或作为列表组合:

       cats = [v for k, v in categories_mapping if k in l]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-31
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-20
        • 1970-01-01
        相关资源
        最近更新 更多