Python 通过在排序列表中合并两个字典的值来组合两个字典
我在编写函数union_collections时遇到问题,该函数使用两个字典d1和d2来表示两个图书集合。该函数生成一个包含d1或d2中所有书籍的新词典,同时保持以下规则: 生成的词典不应包含任何重复的书名。 生成的词典中的每个书名列表都应该使用内置的排序方法进行排序。 无法使用解决方案中键的内置函数 这些是用于测试的样本集合:Python 通过在排序列表中合并两个字典的值来组合两个字典,python,string,algorithm,dictionary,key-value,Python,String,Algorithm,Dictionary,Key Value,我在编写函数union_collections时遇到问题,该函数使用两个字典d1和d2来表示两个图书集合。该函数生成一个包含d1或d2中所有书籍的新词典,同时保持以下规则: 生成的词典不应包含任何重复的书名。 生成的词典中的每个书名列表都应该使用内置的排序方法进行排序。 无法使用解决方案中键的内置函数 这些是用于测试的样本集合: collection1 = \ {'f':['flatland', 'five minute mysteries', 'films of the 19
collection1 = \
{'f':['flatland', 'five minute mysteries', 'films of the 1990s', 'fight club'],
't':['the art of computer programming', 'the catcher in the rye'],
'p':['paradise lost', 'professional blackjack', 'paradise regained'],
'c':['calculus in the real world', 'calculus revisited', 'cooking for one'],
'd':['dancing with cats', 'disaster at midnight']}
collection2 = \
{'f':['flatland', 'films of the 1990s'],
'a':['a brief history of time', 'a tale of two cities'],
'd':['dealing with stress', 'dancing with cats'],
't':['the art of computer programming', 'the catcher in the rye'],
'p':['power and wealth', 'poker essentials', 'post secret'],
'c':['cat couples', 'calculus', 'calculus revisited',
'cooking for one', 'calculus in the real world', 'cooking made easy']}`
例如:独特的集合集合1、集合2应产生:
{'f' : ['fight club' , 'films of the 1990s', 'five minute mysteries', 'flatland'],
't' : ['the art of computer programming', 'the catcher in the rye'],
'p' : ['paradise lost' , 'paradise regained', 'poker essentials', 'post secret' , 'power and wealth', 'professional blackjack'],
'c' : ['calculus' , 'calculus in the real world' , 'calculus revisited' , 'cat couples', 'cooking for one', 'cooking made easy'],
'd' : ['dancing with cats' , 'dealing with stress' , 'disaster at midnight'],
'a' : ['a brief history of time' , 'a tale of two cities']}`
到目前为止,我写了:
def union_集合D1、d2:
联合={}
对于d1或d2中的键:
如果钥匙在d1中且不在d2中:如果钥匙仅在d1中
联合[key]=d1[val]
如果钥匙在d2中,而钥匙不在d1中:
联合[key]=d2[val]
如果输入d1和d2:
union=dictlistd1.items+listd2.items
返回值
此函数不起作用,我不知道如何修复它以符合以下要求
无法导入任何模块。def union_集合D1、d2:
返回{k:sortedlistsetd1.getk,[]+d2.getk,[]
对于setd1.keys+d2.keys}中的k
与上面相同,但尝试更具可读性:
def union_collections(d1, d2):
return { k: sorted(
list(
set(
d1.get(k, []) + d2.get(k, [])
)
)
)
for k in set(d1.keys() + d2.keys()) }
输出:
{'a': ['a brief history of time', 'a tale of two cities'],
'c': ['calculus',
'calculus in the real world',
'calculus revisited',
'cat couples',
'cooking for one',
'cooking made easy'],
'd': ['dancing with cats', 'dealing with stress', 'disaster at midnight'],
'f': ['fight club',
'films of the 1990s',
'five minute mysteries',
'flatland'],
'p': ['paradise lost',
'paradise regained',
'poker essentials',
'post secret',
'power and wealth',
'professional blackjack'],
't': ['the art of computer programming', 'the catcher in the rye']}
代码中的一些问题- 如果执行-union=dictlistd1.items+listd2.items-则认为这是无效的,则无法添加词典。也不需要,这对您的要求没有意义 sorted返回已排序的列表,它不进行就地排序。这将只返回值的排序列表,而不是字典,直接创建字典时需要使用list.sort或sorted函数 对于d1或d2中的键-这仅迭代d1中的键,您需要使用setd1.keys.uniond2.keys d1[val]d2[val]-不正确,没有val变量,请改用d1[key] 对于在两个字典中都找到键的情况,您可以添加两个字典的列表,然后将其转换为set并返回到list,然后对其进行排序,然后将其分配回union字典。范例-
def union_collections(d1, d2):
union = {}
for key in set(d1.keys()).union(d2.keys()):
if key in d1 and key not in d2: # if the key is only in d1
union[key] = d1[key]
if key in d2 and key not in d1:
union[key] = d2[key]
if key in d1 and key in d2:
union[key] = sorted(list(set(d1[key] + d2[key])))
return union
正如评论中所问的那样——
当两个字典中都有一个键时,有没有不使用集合的方法 不用电视机的方法是-
def union_collections(d1, d2):
union = {}
for key in set(d1.keys()).union(d2.keys()):
if key in d1 and key not in d2: # if the key is only in d1
union[key] = d1[key]
if key in d2 and key not in d1:
union[key] = d2[key]
if key in d1 and key in d2:
y = []
union[key] = y
for x in d1[key]:
y.append(x)
for x in d2[key]:
if x not in y:
y.append(x)
y.sort()
return union
您的输入dict的数据结构是什么?我有点困惑,您能在您的字典中给出一个key:value的示例吗?您使用的是哪个版本的Python?字典的keys、values和items方法返回的值在Python2和Python3之间发生了很大的变化,因此假设版本错误的答案可能没有多大帮助,请使用函数和预期输出的示例输入更新帖子。我添加了示例输入和预期输出,但帖子中的代码存在格式错误。如何使函数循环并生成一个字典,其中包含所有更改的值,而不仅仅是“f”或“d”键的值现在是我修复错误的时候了?因为当两个字典中都有一个键时,有没有一种方法可以不使用集合来实现这一点?是的,有,但是如果没有集合,它会更复杂,而且性能可能会更低。你能告诉我这将是怎样的吗?我需要知道如何在没有集合的情况下编写此代码,到目前为止,我已将代码的这部分更改为类似于d2[key]中的d1[key]:如果d1[key]不在union[key]:union[key]。appendd1[key]