简体   繁体   English

从嵌套字典中删除键(Python键)

[英]Remove keys from a nested dict (Python keys)

I'm pretty new in Python, thanks in advance for your help. 我是Python的新手,在此先感谢您的帮助。

I built the following code (I tried the below, I used a dictionary within a dictionary). 我构建了以下代码(我尝试了以下代码,在字典中使用了字典)。

The idea is to keep the keys (hair.color) with values(blonde). 这个想法是保持键(hair.color)与值(金发)。 In this example: remove Micheal. 在此的示例:删除Micheal。

Code: 码:

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")

On People: 对人:

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 "
 }

 }

The code only check the first key: under the condition: values(blonde) ie (people[i]["hair.color"]!=brown) it works just for 1 key and then the code gets "stuck" 该代码仅检查第一个键:在以下条件下: values(blonde)(people[i]["hair.color"]!=brown)它仅适用于一个键,然后代码被“卡住”

My current output: 我当前的输出:

"people"=

 "Michael":
{

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


}
,

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

Instead, I wanted: 相反,我想要:

"people"=

"Michael":
{

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

} 

I want an output, for this case, (only) Michael. 对于这种情况,我想要一个输出(仅)Michael。

You can't delete key while iterating for loop: 您无法在循环迭代时删除密钥:

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"))

OR 要么

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: O / P:

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

you can use list comprehension: 您可以使用列表理解:

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

what is equal to: 等于:

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

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM