simeonshopov

On the Way to Annapurna

Jan 14th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. stores = []
  2.  
  3. command = input()
  4.  
  5. while command != 'END':
  6.   tokens = command.split('->')
  7.   store= tokens[1]
  8.   if tokens[0] == 'Add':
  9.     items= tokens[2].split(',')
  10.     filtered = [x for x in stores if x[0] == store]
  11.     if filtered:
  12.       idx = stores.index(filtered[0])
  13.       stores[idx][1].extend(items)
  14.     else:
  15.       stores.append([store, items])
  16.   elif tokens[0] == 'Remove':
  17.     filtered = [x for x in stores if x[0] == store]
  18.     if filtered:
  19.       idx = stores.index(filtered[0])
  20.       del stores[idx]
  21.   command = input()
  22.  
  23. sorted_stores = sorted(stores, key=lambda x: (len(x[1]), x[0]), reverse=True)
  24. print('Stores list:')
  25. for i in sorted_stores:
  26.   print(i[0])
  27.   [print(f'<<{x}>>') for x in i[1]]
  28.  
  29.  
  30.   #sorted(sorted(stores, key=lambda x: x[0], reverse=True), key=lambda x: len(x), reverse=True)
Advertisement
Add Comment
Please, Sign In to add comment