Collections.OrderedDict()
Collections.OrderedDict()
+ 0 comments I know ordered dictionaries are the norm in modern versions of py3, but added the import just for the spirit of things.
from collections import OrderedDict as od if __name__ == '__main__': n = int(input()) dic = od() for i in range(n): product, price = input().rsplit(' ', 1) dic[product] = int(price) + dic.get(product, 0) print(*(f"{k} {v}" for k, v in dic.items()), sep='\n')
+ 0 comments from collections import OrderedDict N = int(input()) items = OrderedDict() for _ in range(N): item_name, net_price = input().rsplit(maxsplit=1) items[item_name] = items.get(item_name, 0) + int(net_price) for item_name, net_price in items.items(): print(f"{item_name} {net_price}")
+ 0 comments from collections import OrderedDict od = OrderedDict() for i in range(int(input())): item, price = input().rsplit(maxsplit=1) if item in od: od[item] += int(price) else: od[item] = int(price) for items, price in od.items(): print(items, price)
+ 0 comments from collections import OrderedDict
n = int(input()) items = OrderedDict()
for i in range(n): item, price = input().rsplit(' ', 1) net_price = int(price) if item in items: items[item] += net_price else: items[item] = net_price
for item, net_price in items.items(): print(item, net_price)
+ 0 comments from collections import OrderedDict d=OrderedDict() n=int(input()) for i in range(0,n): l=input().rsplit(' ',1) d[l[0]]= int(l[1])+int(d.get(l[0],0)) #print(l,d) for k,v in d.items(): print(k, v)
As i was not sure whats special about orderd dict i searched a bit in stackoverflow and found this gold .Hope this helps you as well
difference-between-dictionary-and-ordereddict
As of Python 3.7, a new improvement to the dict built-in is:
the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec.
This means there is no real need for OrderedDict anymore 🎉. They are almost the same.
Some minor details to consider... Here are some comparisons between Python 3.7+ dict and OrderedDict:
from collections import OrderedDict
d = {'b': 1, 'a': 2} od = OrderedDict([('b', 1), ('a', 2)])
they are equal with content and order
assert d == od assert list(d.items()) == list(od.items()) assert repr(dict(od)) == repr(d) Obviously, there is a difference between the string representation of the two object, with the dict object in more natural and compact form.
str(d) # {'b': 1, 'a': 2} str(od) # OrderedDict([('b', 1), ('a', 2)]) As for different methods between the two, this question can be answered with set theory:
d_set = set(dir(d)) od_set = set(dir(od)) od_set.difference(d_set)
{'dict', 'reversed', 'move_to_end'} for Python 3.7
{'dict', 'move_to_end'} for Python 3.8+
This means OrderedDict has at most two features that dict does not have built-in, but work-arounds are shown here:
Workaround for reversed / reversed() No workaround is really needed for Python 3.8+, which fixed this issue. OrderedDict can be "reversed", which simply reverses the keys (not the whole dictionary):
reversed(od) # list(reversed(od)) # ['a', 'b']
with Python 3.7:
reversed(d) # TypeError: 'dict' object is not reversible list(reversed(list(d.keys()))) # ['a', 'b']
with Python 3.8+:
reversed(d) # list(reversed(d)) # ['a', 'b'] To properly reverse a whole dictionary using Python 3.7+:
dict(reversed(list(d.items()))) # {'a': 2, 'b': 1} Workaround for move_to_end OrderedDict has a move_to_end method, which is simple to implement:
od.move_to_end('b') # now it is: OrderedDict([('a', 2), ('b', 1)])
d['b'] = d.pop('b') # now it is: {'a': 2, 'b': 1}
Sort 534 Discussions, By:
Please Login in order to post a comment