【问题标题】:Remove keys from a nested dict (Python keys)从嵌套字典中删除键(Python 键)
【发布时间】:2019-11-18 21:56:57
【问题描述】:

我是 Python 新手,提前感谢您的帮助。

我构建了以下代码(我尝试了以下代码,我在字典中使用了字典)。

这个想法是保持键(hair.color)和值(金发)。在此示例中:删除 Micheal。

代码:

def answers(hair_questions):
    try:
        for i in people:
            if people[i]["hair.color"]==hair_questions:
                print(people[i])
            else:
                del people[i]
            return people[i]
    except:
        print("Doesn´t exist")

answers("brown")

关于人:

people={
 "Anne":
    {
   "gender":"female",
   "skin.color":"white",
  "hair.color":"blonde",
  "hair.shape":"curly"
 }
,
"Michael":
{

  "citizenship":"africa",
  "gender":"male",
  "hair.color":"brown",
  "hair.shape":"curly"


}
,

"Ashley":
    {
  "gender":"female",
  "citizenship":"american",
  "hair.color":"blonde",
  "hair.shape":"curly "
 }

 }

代码仅检查第一个键:在条件下:values(blonde) 即(people[i]["hair.color"]!=brown) 它仅适用于 1 个键,然后代码“卡住”

我目前的输出:

"people"=

 "Michael":
{

  "citizenship":"africa",
  "gender":"male",
  "hair.color":"brown",
  "hair.shape":"curly"


}
,

"Ashley":
    {
  "gender":"female",
  "citizenship":"american",
  "hair.color":"blonde",
  "hair.shape":"curly "
 }

相反,我想要:

"people"=

"Michael":
{

  "citizenship":"africa",
  "gender":"male",
  "hair.color":"brown",
  "hair.shape":"curly"

} 

我想要一个输出,在这种情况下,(仅)Michael。

【问题讨论】:

  • 您是否严格需要删除键,或者创建一个只包含所需键的新字典就足够了?
  • 丢掉 else 部分,你就可以开始了
  • 如果你只想要hair.color == brown的那些,我也不确定为什么Michael会出现在输出中

标签: python string dictionary


【解决方案1】:

在 for 循环迭代时不能删除键:

people={
    "Anne":
        {
       "gender":"female",
       "skin.color":"white",
      "hair.color":"blonde",
      "hair.shape":"curly"
     },
    "Michael":
    {
      "citizenship":"africa",
      "gender":"male",
      "hair.color":"brown",
      "hair.shape":"curly"
    },
    "Ashley":
        {
          "gender":"female",
          "citizenship":"american",
          "hair.color":"blonde",
          "hair.shape":"curly "
        }
 }

def answers(hair_questions):
    my_dict = {}
    for i in people:
        if people[i]["hair.color"] in hair_questions:
            my_dict[i] = people[i]
    return  my_dict

print(answers("brown"))

或

def answers(hair_questions):
    my_list = []
    for i in people:
        if people[i]["hair.color"] not in hair_questions:
            my_list.append(i)

    for i in my_list:
        del people[i]

answers("brown")
print(people)

O/P:

{'Michael': {'citizenship': 'africa', 'gender': 'male', 'hair.color': 'brown', 'hair.shape': 'curly'}}

【讨论】:

    【解决方案2】:

    您可以使用列表推导:

    brown = {key:value for key,value in people.items() if people[key]["hair.color"] != "blonde"}
    print (brown)
    

    等于:

    brown= {}
    for key,value in people.items():
        if people[key]["hair.color"] != "blonde":
            brown[key] = value
    print (brown)
    

    输出:

    {'Michael': {'citizenship': 'africa', 'gender': 'male', 'hair.color': 'brown', 'hair.shape': 'curly'}}
    

    【讨论】:

      猜你喜欢
      • 2017-03-08
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多