“Python Sort Dict berdasarkan nilai” Kode Jawaban

Python Urutkan Kamus berdasarkan Nilai

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

sort_by_key = dict(sorted(x.items(),key=lambda item:item[0]))
sort_by_value = dict(sorted(x.items(), key=lambda item: item[1]))

print("sort_by_key:", sort_by_key)
print("sort_by_value:", sort_by_value)

# sort_by_key: {0: 0, 1: 2, 2: 1, 3: 4, 4: 3}
# sort_by_value: {0: 0, 2: 1, 1: 2, 4: 3, 3: 4}
Clear Camel

Sortir Daftar Kamus Python berdasarkan Nilai

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

Python - Urutkan Kamus berdasarkan Nilai

d = {'one':1,'three':3,'five':5,'two':2,'four':4}

# Sort
a = sorted(d.items(), key=lambda x: x[1])

# Reverse sort
r = sorted(d.items(), key=lambda x: x[1], reverse=True)
Andrea Perlato

cara mengurutkan kamus berdasarkan nilai dalam python

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))


# Sort by key
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
Mobile Star

cara mengurutkan kamus dalam python berdasarkan nilai

# your dictionary
a = {'a':4, 'c':5, 'b':3, 'd':0}

# sort x by keys
a_keys = dict(sorted(a.items(),key=lambda x:x[0],reverse = False)) # ascending order
# output: {'a': 4, 'b': 3, 'c': 5, 'd': 0}

# # sort x by values
a_values = dict(sorted(a.items(),key=lambda x:x[1],reverse = False)) # ascending order
# output: {'d': 0, 'b': 3, 'a': 4, 'c': 5}
Darkstar

Python Sort Dict berdasarkan nilai

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
{k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
rennmaushunter

Jawaban yang mirip dengan “Python Sort Dict berdasarkan nilai”

Pertanyaan yang mirip dengan “Python Sort Dict berdasarkan nilai”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya