【问题标题】:extraction of dictionary in python在python中提取字典
【发布时间】:2022-08-02 16:39:14
【问题描述】:

我创建了一个标志颜色字典:

flag_colours={\'uk\': [\'blue\',\'white\', \'red\'],
          \'france\': [\'blue\', \'white\', \'red\'],
          \'italy\': [\'green\', \'white\', \'red\'],
          \'switzerland\': [\'red\', \'white\']}          

我想提取国旗中有两种以上颜色的国家,但我不确定如何添加它:我目前有 [x for x in flag_colours if \'\' in x] 用于提取但不确定添加什么

    标签: python dictionary extract


    【解决方案1】:

    您可以使用带有条件的列表推导,例如:

    [country for country, colours in flag_colours.items() if len(colours) > 2]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-12
      • 2019-03-02
      • 2013-08-09
      • 2017-02-09
      • 2020-05-11
      • 2017-12-05
      • 2016-09-15
      • 2020-09-23
      相关资源
      最近更新 更多