如果键相同,则在空字典的列表中添加键值对
Adding a Key-Value pair in a list in an empty dictionary if the keys are the same
如果键相等,如何将字典打印为给定字典的并集并附加值?
输入:
dict_1 = {
"x":[1,"hello"],
"y":[2,"world"],
"z":3
}
dict_2 ={
"p":4,
"q":19,
"z":123
}
输出:
dict_3={"x":[1,"hello"],
"y":[2,"world"],
"z":[3,123],
"p":4,
"q":19,
}
试试这个:查看内联评论以了解正在发生的事情。
for key,value in dict1.items(): # loop through dict1
if key in dict2: # see if each key in dict1 exists in dict2
if isinstance(dict2[key], list): # if it is in dict2 check if the value is a list.
dict2[key].append(value) # if it is a list append the dict1 value
else: # otherwise
dict2[key] = [dict2[key], value] # create a new list and stick
# the values for dict1 and
# dict2 inside of it
else: # if the key is not in dict2
dict2[key] = value # set the new key and value in dict2
print(dict2) # output
如果你想检查其他集合试试这个
for key,value in dict1.items(): # loop through dict1
if key in dict2: # see if each key in dict1 exists in dict2
if hasattr(dict2[key], '__iter__'): # if it is in dict2 check if the value is a list.
if value not in dict2[key]:
try:
dict2[key].append(value)
except:
dict2[key].add(value)
finally:
dict2[key] = tuple(list(dict2[key]) + [value])
else: # otherwise
dict2[key] = [dict2[key], value] # create a new list and stick
# the values for dict1 and
# dict2 inside of it
else: # if the key is not in dict2
dict2[key] = value # set the new key and value in dict2
print(dict2) # output
试试这个
# use the union of the keys and construct a list if a key exists in both dicts
# otherwise get the value of the key from the dict it exists in
{k:[dict_1[k], dict_2[k]] if all(k in d for d in [dict_1, dict_2]) else (dict_1.get(k) or dict_2.get(k)) for k in set(dict_1).union(dict_2)}
# {'x': [1, 'hello'], 'q': 19, 'y': [2, 'world'], 'z': [3, 123], 'p': 4}
可以使用 next
和 walrus operator
来编写(希望)更易读一些:
{k: next(i for i in v if i is not None) if None in (v:=[dict_1.get(k), dict_2.get(k)]) else v for k in set(dict_1).union(dict_2)}
与普通循环相同的代码:
dict_3 = {}
# loop over the union of the keys
for k in set(dict_1).union(dict_2):
# construct the list from the values
v = [dict_1.get(k), dict_2.get(k)]
# if a key doesn't exist in one of the dicts, it will dict.get() will give None
if None in v:
# in which case, get the non-None value
dict_3[k] = next(i for i in v if i is not None)
# otherwise, i.e. a key exists in both dicts,
else:
dict_3[k] = v
如果键相等,如何将字典打印为给定字典的并集并附加值?
输入:
dict_1 = {
"x":[1,"hello"],
"y":[2,"world"],
"z":3
}
dict_2 ={
"p":4,
"q":19,
"z":123
}
输出:
dict_3={"x":[1,"hello"],
"y":[2,"world"],
"z":[3,123],
"p":4,
"q":19,
}
试试这个:查看内联评论以了解正在发生的事情。
for key,value in dict1.items(): # loop through dict1
if key in dict2: # see if each key in dict1 exists in dict2
if isinstance(dict2[key], list): # if it is in dict2 check if the value is a list.
dict2[key].append(value) # if it is a list append the dict1 value
else: # otherwise
dict2[key] = [dict2[key], value] # create a new list and stick
# the values for dict1 and
# dict2 inside of it
else: # if the key is not in dict2
dict2[key] = value # set the new key and value in dict2
print(dict2) # output
如果你想检查其他集合试试这个
for key,value in dict1.items(): # loop through dict1
if key in dict2: # see if each key in dict1 exists in dict2
if hasattr(dict2[key], '__iter__'): # if it is in dict2 check if the value is a list.
if value not in dict2[key]:
try:
dict2[key].append(value)
except:
dict2[key].add(value)
finally:
dict2[key] = tuple(list(dict2[key]) + [value])
else: # otherwise
dict2[key] = [dict2[key], value] # create a new list and stick
# the values for dict1 and
# dict2 inside of it
else: # if the key is not in dict2
dict2[key] = value # set the new key and value in dict2
print(dict2) # output
试试这个
# use the union of the keys and construct a list if a key exists in both dicts
# otherwise get the value of the key from the dict it exists in
{k:[dict_1[k], dict_2[k]] if all(k in d for d in [dict_1, dict_2]) else (dict_1.get(k) or dict_2.get(k)) for k in set(dict_1).union(dict_2)}
# {'x': [1, 'hello'], 'q': 19, 'y': [2, 'world'], 'z': [3, 123], 'p': 4}
可以使用 next
和 walrus operator
来编写(希望)更易读一些:
{k: next(i for i in v if i is not None) if None in (v:=[dict_1.get(k), dict_2.get(k)]) else v for k in set(dict_1).union(dict_2)}
与普通循环相同的代码:
dict_3 = {}
# loop over the union of the keys
for k in set(dict_1).union(dict_2):
# construct the list from the values
v = [dict_1.get(k), dict_2.get(k)]
# if a key doesn't exist in one of the dicts, it will dict.get() will give None
if None in v:
# in which case, get the non-None value
dict_3[k] = next(i for i in v if i is not None)
# otherwise, i.e. a key exists in both dicts,
else:
dict_3[k] = v