“Python Sort Dict by Key” Kode Jawaban

Urutkan Kamus Python

l = {1: 40, 2: 60, 3: 50, 4: 30, 5: 20}
d1 = dict(sorted(l.items(),key=lambda x:x[1],reverse=True))
print(d1) #output : {2: 60, 3: 50, 1: 40, 4: 30, 5: 20}
d2 = dict(sorted(l.items(),key=lambda x:x[1],reverse=False))
print(d2) #output : {5: 20, 4: 30, 1: 40, 3: 50, 2: 60}
Rajanit Navapara

Sortir Daftar Kamus dengan Key Python

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 
Stupid Stork

Urutkan kamus dalam python

d = {2: 3, 1: 89, 4: 5, 3: 0}
od = sorted(d.items())
print(od)
Clean Cicada

Python Dict memerintahkan dikts by kunci

d1 = dict(sorted(d.items(), key = lambda x:x[0]))
Dull Donkey

Urutkan Kamus

#for dictionary d
sorted(d.items(), key=lambda x: x[1]) #for inceasing order
sorted(d.items(), key=lambda x: x[1], reverse=True) # for decreasing order
#it will return list of key value pair tuples
Annoying Anteater

Python Sort Dict by Key

A={1:2, -1:4, 4:-20}
{k:A[k] for k in sorted(A)}

output:
{-1: 4, 1: 2, 4: -20}
IJustWannaHelp

Jawaban yang mirip dengan “Python Sort Dict by Key”

Pertanyaan yang mirip dengan “Python Sort Dict by Key”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya