Collections.OrderedDict()

Sort by

recency

|

730 Discussions

|

  • + 0 comments
    from collections import OrderedDict
    
    n = int(input())
    od = OrderedDict()
    
    for _ in range(n):
        item = input().split()
        item_name = ' '.join(item[:-1])
        item_price = int(item[-1])
        if item_name in od:
            od[item_name] += item_price
        else:
            od[item_name] = item_price
    
    for item in od:
        print(f'{item} {od[item]}')
    
  • + 0 comments
    from collections import OrderedDict
    
    n = int(input())
    
    od = OrderedDict()
    
    for _ in range(n):
        
        *name_parts, item_price = input().split(' ')
        
        item_name = " ".join(name_parts)
        
        if item_name in od:
            
            od[item_name] += int(item_price)
            
        else:
            
            od[item_name] = int(item_price)
    
    for name, total in od.items():
        
        print(f'{name} {total}')
    
  • + 0 comments
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    from collections import OrderedDict
    N = int(input())
    
    d1 = OrderedDict()
    for _ in range(N):
        *key, value = input().split()
        key = ' '.join(key)
        value = int(value)
        if key in d1.keys():
            d1[key] = d1[key] + value
        else:
            d1[key] = value
    
    for k,v in d1.items():
        print(k,v)
    
  • + 0 comments
    ordDict = OrderedDict()
    
    for line in sys.stdin:
        strippedItem = line.strip()    
        splitItem = strippedItem.rsplit(" ", 1)    
        if not strippedItem.isnumeric(): 
            if splitItem[0] not in ordDict.keys():
                ordDict[splitItem[0]] = int(splitItem[1])
            else:
                ordDict[splitItem[0]] += int(splitItem[1])
            
    for key in ordDict.keys():
        print(key + " " + str(ordDict.get(key)))
    
  • + 0 comments
    from collections import OrderedDict
    n = int(input())
    ItemPriceDic = OrderedDict()
    for _ in range(n):
        Inp = input().split()
        item_name = ' '.join(Inp[:-1])
        item_price = int(Inp[-1])
        if item_name in ItemPriceDic.keys():
            ItemPriceDic[item_name] += item_price
        else:
            ItemPriceDic[item_name] = item_price
            
    for i in ItemPriceDic:
        print(' '.join(map(str,(i,ItemPriceDic[i]))))