您可以在下面看到一个示例。我希望你的意思是类似的解决方案。
test_ids_1 = [1, 2, 3, 4, 5, 6]
test_ids_2 = [1, 2, 3, 4, 5, 8]
json_var = [
{
"human": {"man":
{"workers": [1, 2, 3],
"blabla": "abcd"},
"woman":
{"workers": [4, 5, 6],
"blabla": "dcab"}}
}
]
for worker in json_var[0]["human"]:
for ids in json_var[0]["human"][worker]["workers"]:
if ids not in test_ids_1:
print("%s is missing from test_ids_1 list" % ids)
if ids not in test_ids_2:
print("%s is missing from test_ids_2 list" % ids)
输出:
python test.py
6 is missing from test_ids_2 list
注意:当然,如果您读取 Json 文件并对其进行序列化,那么您可以对其进行迭代并检查元素。您可以在此链接上查看详细信息:https://docs.python.org/2/library/json.html