Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dict_sortnearest.py
- lista = [16,11,24,25]
- dica = {'a':[10],'b':[20],'c':[30]}
- k,v = zip(*[(dica[k][0],k) for k in dica])
- for num in lista:
- z = [abs(x-num) for x in k]
- dica[v[z.index(min(z))]].append(num)
- print(dica)
- # ... or a faster way for Python to process as by only indexing...
- dica = {0:[10],1:[20],2:[30]}
- k = [dica[k][0] for k in dica]
- for num in lista:
- z = [abs(x-num-1) for x in k]
- dica[z.index(min(z))].append(num)
- print(dica)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement