我正在尝试相互对照两个字典。目的是确认list1中的country
也存在于list2中。如果不存在,则应打印country
不存在(如果存在),它将打印出该国家存在。
两个列表均以国家/地区开头,但长度随键和值的不同而不同,如下所示。
list1 = [{ 'country': 'brazl', 'state': 'oregon'}, { 'country': 'japan', 'state': 'chicago'}, { 'country': 'australia', 'state': 'washington', 'water': 'clean'} { 'country':'china']
list2 = ...
答案 0 :(得分:2)
例如,您可以将列表1中的所有国家/地区设为一组,而将列表2中的国家/地区设为另一组:
countries1 = set(item.get("country", "unknown") for item in list1)
countries2 = set(item.get("country", "unknown") for item in list2)
然后将两者区别开来:
print("countries in list1 but not in list2:")
print(countries1 - countries2)
或工会:
print("countries in list1 and in list2:")
print(countries1 & countries2)
答案 1 :(得分:1)
制作每个列表中存在的国家/地区集,然后减去它们,从而在list1中(而不是list2)中有一组国家/地区,例如:
missing_countries = set([i['country'] for i in list1]) - set([i['country'] for i in list2])
print("Countries not in list2:")
for i in missing_countries:
print(i)
答案 2 :(得分:0)
countries1 = list(item['country'] for item in list1)
countries2 = list(item['country'] for item in list2)
compare1 = [country for country in countries1 if country not in countries2] # doesnt exist
compare2 = [country for country in countries1 if country in countries2] # exist
案例状态:
state1 = list(item['state'] for item in list1)
state2 = list(item['state'] for item in list2)
compare1 = [state for state in state1 if state not in state2] # doesnt exist
更新的代码:
list1 = [{ 'country': 'brazil', 'state': 'oregon' , 'some_key': 'some _value'}, { 'country': 'japan', 'state': 'chicago'}, { 'country': 'australia', 'state': 'washington', 'water': 'clean'}, { 'country':'china', 'state':'Tokio'}]
list2 = [{ 'country': 'brazil', 'state': 'oregon'}, { 'country': 'america', 'state': 'new york'}, { 'country': 'australia', 'state': 'washington', 'water': 'clean'},{ 'country':'china'}]
def check_key(dict_key):
# Check if key exist in all dictionaries
for item in list1+list2:
if dict_key in item.keys():
status = True
else:
status = False
return status
return status
# If key exist in all dictionaries compare
# If key doesnt exist in all dictionaries skip to next key
for k in list1[0].keys():
# Ckeck if key exist in all dictionaries
status = check_key(k)
# test
#print ("key: {}, exist in all".format(k)) if status else print ("key: {}, doesn't exist in all".format(k)) # key: country, exist in all
if status: # True
# If key exist in all dictionaries
values1 = list(item[k] for item in list1)
values2 = list(item[k] for item in list2)
compare1 = [country for country in values1 if country not in values2] # doesnt exist
print ("For key: {}, values which doesnt exist in list2: {}".format(k,compare1))
compare2 = [country for country in countries1 if country in countries2] # exist
print("For key: {}, values which exist in list2: {}".format(k, compare2))
else: # False
# If key doesnt exist in all dictionaries
pass
输出:
For key: country, values which doesnt exist in list2: ['japan']
For key: country, values which exist in list2: ['brazil', 'australia', 'china']
更新代码2 :):
list1 = [{ 'country': 'brazl', 'state': 'oregon'}, { 'country': 'japan', 'state': 'chicago'}, { 'country': 'australia', 'state': 'washington', 'water': 'clean'},{'country':'china'}]
for item in list1:
if 'state' in item.keys():
print (item['state'])
输出:
oregon
chicago
washington