Python 局部变量可能在赋值之前被引用
嗨,我有两本字典1。小学,2。次要的 需要检查两个字典的第一个字段Python 局部变量可能在赋值之前被引用,python,dictionary,Python,Dictionary,嗨,我有两本字典1。小学,2。次要的 需要检查两个字典的第一个字段 primary = {"Latest":[ { "name": "Employee", "field": "employee", "values": [ { "title": "A", "p
primary = {"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"count": "1"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"count": "3"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "1"
}
]
}
]}
secondary = [
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "Test",
"count": "null"
},
{
"title": "B",
"paragraph": "B",
"count": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "test",
"count": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "null"
}
]
}
]
如果字段相同,则将标题与主标题和次标题进行比较
*若字段和标题相同,则从主字典向辅助字典添加计数
primary = {"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"count": "1"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"count": "3"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "1"
}
]
}
]}
secondary = [
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "Test",
"count": "null"
},
{
"title": "B",
"paragraph": "B",
"count": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "test",
"count": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "null"
}
]
}
]
初级词典
primary = {"Latest":[
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "null",
"count": "1"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "null",
"count": "3"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "1"
}
]
}
]}
secondary = [
{
"name": "Employee",
"field": "employee",
"values": [
{
"title": "A",
"paragraph": "Test",
"count": "null"
},
{
"title": "B",
"paragraph": "B",
"count": "null"
}
]
},
{
"name": "Project",
"field": "project",
"values": [
{
"title": "NEW_York",
"paragraph": "test",
"count": "null"
}
]
},
{
"name": "Designation",
"field": "designation",
"values": [
{
"title": "Developer",
"paragraph": "null",
"count": "null"
}
]
}
]
代码如下
def test(second,primary):
for secondary_value in second:
for value in primary:
if secondary_value['title'] == value['title']:
secondary_value['count'] = value['count']
return secondary_value
for primary in primary['Latest']:
for secondary_elem in secondary:
if secondary_elem['field'] == primary['field']:
test(secondary_elem['values'],primary['values'])
secondary
上面的代码工作正常,但它在ide中显示了逻辑错误。在分配如何修复此问题之前,可能会引用局部变量IDE不应使用此代码向您发出此警告。您使用哪个IDE?pycharm是一个IDE。我将您的代码复制到pycharm 2020.2.3,但没有收到这样的消息。(我甚至用一个额外的变量修改了代码,以查看此消息在我的配置中是否没有被抑制,并且工作正常)return second修复了我的问题为什么
test
会返回任何东西?修改其参数/所有者的函数/方法通常不返回任何值/None
(例如list.sort
)或非参数/所有者值(例如dict.pop
)。如果second
为空,则会生成原始代码中的警告,然后,secondary\u value
将是一个未定义的名称,因为不会执行任何循环。
def test(second,primary):
for secondary_value in second:
for value in primary:
if secondary_value['title'] == value['title']:
secondary_value['count'] = value['count']
return second
for primary in primary['Latest']:
for secondary_elem in secondary:
if secondary_elem['field'] == primary['field']:
test(secondary_elem['values'],primary['values'])
secondary