简体   繁体   English

局部变量可能在赋值前被引用

[英]Local Variable might ne referenced before assignment

Hi I have two dictionaries 1.Primary, 2. Secondary嗨,我有两本词典 1.Primary,2.Secondary

Need to check first field of both dictionary需要检查两个字典的第一个字段

If field is same compare the title with primary and secondary如果字段相同,则比较标题与主要和次要

*If field and title is same then From Primary dictionary add count to secondary dictionary *如果字段和标题相同,则从主词典将计数添加到辅助词典

Primary dictionary初级词典

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

Code is below代码如下

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

Above code is working fine, but it showing logical error in ide. Local Variable might ne referenced before assignment How to fix this上面的代码工作正常,但它在 ide 中显示逻辑错误。局部变量可能在分配之前被引用如何解决这个问题

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

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

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