Hi I have two dictionaries 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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.