python - Combining two dictionaries by merging its values in a sorted list -
i'm having trouble writing function union_collections consumes 2 dictionaries (d1 , d2) representing 2 book collections. function produces new dictionary containing books present in d1 or d2, while maintaining following rules:
- the produced dictionary should not contain duplicate book titles.
- each list of book titles within produced dictionary should sorted using built- in sort() method.
- cannot use built-in function fromkeys() in solution
these sample collections testing:
collection1 = \ {'f':['flatland', 'five minute mysteries', 'films of 1990s', 'fight club'], 't':['the art of computer programming', 'the catcher in rye'], 'p':['paradise lost', 'professional blackjack', 'paradise regained'], 'c':['calculus in real world', 'calculus revisited', 'cooking one'], 'd':['dancing cats', 'disaster @ midnight']} collection2 = \ {'f':['flatland', 'films of 1990s'], 'a':['a brief history of time', 'a tale of 2 cities'], 'd':['dealing stress', 'dancing cats'], 't':['the art of computer programming', 'the catcher in rye'], 'p':['power , wealth', 'poker essentials', 'post secret'], 'c':['cat couples', 'calculus', 'calculus revisited', 'cooking one', 'calculus in real world', 'cooking made easy']}`
an example: unique_collections(collection1, collection2)
should produce:
{'f' : ['fight club' , 'films of 1990s', 'five minute mysteries', 'flatland'], 't' : ['the art of computer programming', 'the catcher in rye'], 'p' : ['paradise lost' , 'paradise regained', 'poker essentials', 'post secret' , 'power , wealth', 'professional blackjack'], 'c' : ['calculus' , 'calculus in real world' , 'calculus revisited' , 'cat couples', 'cooking one', 'cooking made easy'], 'd' : ['dancing cats' , 'dealing stress' , 'disaster @ midnight'], 'a' : ['a brief history of time' , 'a tale of 2 cities']}`
so far i've written:
def union_collections(d1, d2): union = {} key in d1 or d2: if key in d1 , key not in d2: # if key in d1 union[key] = d1[val] if key in d2 , key not in d1: # union[key] = d2[val] if key in d1 , key in d2: union = dict(list(d1.items()) + list(d2.items())) return sorted(union.values())
this function isn't working , have no idea how fix adhere following requirements.
cannot import modules.
some issues in code -
when -
union = dict(list(d1.items()) + list(d2.items()))
- not think valid, cannot add dictionaries. , not need well, not make sense requirement.sorted()
returns sorted list, not in place sorting. , return sorted list of values , not dictionary, need uselist.sort()
orsorted()
function when creating dictionaries directly.for key in d1 or d2
- iterates on keys in d1, need useset(d1.keys()).union(d2.keys())
.d1[val]
(d2[val]
) - not correct, there notval
variable, used1[key]
instead.
for case key found in both dictionary, can add lists of both dictionaries , convert set , list , sort , assign union
dictionary. example -
def union_collections(d1, d2): union = {} key in set(d1.keys()).union(d2.keys()): if key in d1 , key not in d2: # if key in d1 union[key] = d1[key] if key in d2 , key not in d1: union[key] = d2[key] if key in d1 , key in d2: union[key] = sorted(list(set(d1[key] + d2[key]))) return union
as asked in comments -
for when key in both dictionaries, there way without use of sets?
the way without sets -
def union_collections(d1, d2): union = {} key in set(d1.keys()).union(d2.keys()): if key in d1 , key not in d2: # if key in d1 union[key] = d1[key] if key in d2 , key not in d1: union[key] = d2[key] if key in d1 , key in d2: y = [] union[key] = y x in d1[key]: y.append(x) x in d2[key]: if x not in y: y.append(x) y.sort() return union
Comments
Post a Comment