Advertisement
here2share

# dict_sortnearest.py

Dec 13th, 2019
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. # dict_sortnearest.py
  2.  
  3. lista = [16,11,24,25]
  4. dica = {'a':[10],'b':[20],'c':[30]}
  5.  
  6. k,v = zip(*[(dica[k][0],k) for k in dica])
  7. for num in lista:
  8.     z = [abs(x-num) for x in k]
  9.     dica[v[z.index(min(z))]].append(num)
  10. print(dica)
  11.  
  12. # ... or a faster way for Python to process as by only indexing...
  13. dica = {0:[10],1:[20],2:[30]}
  14.  
  15. k = [dica[k][0] for k in dica]
  16. for num in lista:
  17.     z = [abs(x-num-1) for x in k]
  18.     dica[z.index(min(z))].append(num)
  19. print(dica)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement