有谁知道为什么我没有得到输出,代码编译但 returns 没有输出
Does anyone know why i do not get an output, the code does compile but returns no output
employees =[
{
"name": "John Doe",
"job": "Software Engineer",
"City": "Vancouver",
"age": 34,
"status": "single"
},
{
"name": "Alicia Smith",
"job": "Director",
"City": "New York",
"age": 38,
"status": "Married"
}
]
keys = ["name", "age"]
if "name" and "age" in employees:
newDict = employees.pop("name" and "age")
print(newDict)
首先你在代码中遇到了两个错误,第一个是因为 if 语句不正确,这就是为什么没有被执行的原因。其次,你不能在数组中弹出字典,你必须选择每个字典,然后弹出它们或将它们放入循环中,就像这个解决方案
employees =[ { "name": "John Doe", "job": "Software Engineer", "City": "Vancouver", "age": 34, "status": "single" }, { "name": "Alicia Smith", "job": "Director", "City": "New York", "age": 38, "status": "Married" } ]
newDict = []
for i in employees:
if "name" and "age" in i:
i.pop("name" and "age")
newDict.append(i)
print(newDict)
您的代码中有一些错误。我相信您正试图从存在的字典中删除键 name
和 age
。请尝试以下操作:
employees = [
{
"name": "John Doe",
"job": "Software Engineer",
"City": "Vancouver",
"age": 34,
"status": "single"
},
{
"name": "Alicia Smith",
"job": "Director",
"City": "New York",
"age": 38,
"status": "Married"
}
]
newDict = []
for d in employees: # First iterate over the dictionaries
if "name" in d: # If the key "name" is there in the dictionary
d.pop("name") # Remove it
if "age" in d: # Check if "age" is there in the keys
d.pop("age") # Remove it too if present
newDict.append(d) # Add the updated dictionary to our list
print(newDict)
您可以使用 dict comprehension inside a list comprehension 作为单行。
print([{k: v for k, v in d.items() if k not in keys} for d in employees])
如果你想在多行中执行它,你应该在从字典中删除项目之前复制字典。否则,您将收到以下错误。
RuntimeError: dictionary changed size during iteration
employees =[
{
"name": "John Doe",
"job": "Software Engineer",
"City": "Vancouver",
"age": 34,
"status": "single"
},
{
"name": "Alicia Smith",
"job": "Director",
"City": "New York",
"age": 38,
"status": "Married"
}
]
keys = ["name", "age"]
if "name" and "age" in employees:
newDict = employees.pop("name" and "age")
print(newDict)
首先你在代码中遇到了两个错误,第一个是因为 if 语句不正确,这就是为什么没有被执行的原因。其次,你不能在数组中弹出字典,你必须选择每个字典,然后弹出它们或将它们放入循环中,就像这个解决方案
employees =[ { "name": "John Doe", "job": "Software Engineer", "City": "Vancouver", "age": 34, "status": "single" }, { "name": "Alicia Smith", "job": "Director", "City": "New York", "age": 38, "status": "Married" } ]
newDict = []
for i in employees:
if "name" and "age" in i:
i.pop("name" and "age")
newDict.append(i)
print(newDict)
您的代码中有一些错误。我相信您正试图从存在的字典中删除键 name
和 age
。请尝试以下操作:
employees = [
{
"name": "John Doe",
"job": "Software Engineer",
"City": "Vancouver",
"age": 34,
"status": "single"
},
{
"name": "Alicia Smith",
"job": "Director",
"City": "New York",
"age": 38,
"status": "Married"
}
]
newDict = []
for d in employees: # First iterate over the dictionaries
if "name" in d: # If the key "name" is there in the dictionary
d.pop("name") # Remove it
if "age" in d: # Check if "age" is there in the keys
d.pop("age") # Remove it too if present
newDict.append(d) # Add the updated dictionary to our list
print(newDict)
您可以使用 dict comprehension inside a list comprehension 作为单行。
print([{k: v for k, v in d.items() if k not in keys} for d in employees])
如果你想在多行中执行它,你应该在从字典中删除项目之前复制字典。否则,您将收到以下错误。
RuntimeError: dictionary changed size during iteration