📌  相关文章
📜  Python|按键或值对Python字典进行排序

📅  最后修改于: 2022-05-13 01:54:31.655000             🧑  作者: Mango

Python|按键或值对Python字典进行排序

先决条件:

  • 字典
  • 列表
  • 合并两个字典
  • 字典方法

问题陈述——以下是需要执行的主要任务。

  1. 创建字典并按字母顺序显示其键。
  2. 显示按键的字母顺序排序的键和值。
  3. 与第 (ii) 部分相同,但按值按字母顺序排序。

方法 -
加载 Dictionary 并执行以下操作:

  1. 首先,使用key_value.iterkeys()函数按字母顺序对键进行排序。
  2. 其次,使用sorted (key_value)函数按字母顺序对键进行排序并打印与其对应的值。
  3. 第三,使用key_value.iteritems() 按字母顺序对值进行排序,key = lambda (k, v) : (v, k))

让我们尝试执行上述任务:

  1. 按字母顺序显示键:
    例子:
Input:
key_value[2] = '64'       
key_value[1] = '69'
key_value[4] = '23'
key_value[5] = '65'
key_value[6] = '34'
key_value[3] = '76'

Output:
1 2 3 4 5 6 
  1. 程序:
Python3
# Function calling
def dictionairy():
 # Declare hash function     
 key_value ={}   
 
# Initializing value
 key_value[2] = 56      
 key_value[1] = 2
 key_value[5] = 12
 key_value[4] = 24
 key_value[6] = 18     
 key_value[3] = 323
 
 print ("Task 1:-\n")
 print ("Keys are")
  
 # iterkeys() returns an iterator over the
 # dictionary’s keys.
 for i in sorted (key_value.keys()) :
     print(i, end = " ")
 
def main():
    # function calling
    dictionairy()            
     
# Main function calling
if __name__=="__main__":     
    main()


Python3
# function calling
def dictionairy():
 
 # Declaring the hash function     
 key_value ={}   
  
# Initialize value
 key_value[2] = 56      
 key_value[1] = 2
 key_value[5] = 12
 key_value[4] = 24
 key_value[6] = 18     
 key_value[3] = 323
  
 print ("Task 2:-\nKeys and Values sorted in",
            "alphabetical order by the key  ") 
 
 # sorted(key_value) returns an iterator over the
 # Dictionary’s value sorted in keys. 
 for i in sorted (key_value) :
    print ((i, key_value[i]), end =" ")
 
def main():
    # function calling
    dictionairy()            
     
# main function calling
if __name__=="__main__":    
    main()


Python3
# Function calling
def dictionairy():
 
 # Declaring hash function     
 key_value ={}   
  
# Initializing the value
 key_value[2] = 56      
 key_value[1] = 2
 key_value[5] = 12
 key_value[4] = 24
 key_value[6] = 18     
 key_value[3] = 323
  
 
 print ("Task 3:-\nKeys and Values sorted",
   "in alphabetical order by the value")
  
 # Note that it will sort in lexicographical order
 # For mathematical way, change it to float
 print(sorted(key_value.items(), key =
             lambda kv:(kv[1], kv[0])))   
  
def main():
    # function calling
    dictionairy()           
     
# main function calling
if __name__=="__main__":     
    main()


Python3
# Creates a sorted dictionary (sorted by key)
from collections import OrderedDict
 
dict = {'ravi':'10','rajnish':'9','sanjeev':'15','yash':'2','suraj':'32'}
dict1 = OrderedDict(sorted(dict.items()))
print(dict1)


输出:
Task 1:-

Keys are
1 2 3 4 5 6

  1. 使用键按字母顺序对键和值进行排序。
    例子:
Input:
key_value[2] = '64'       
key_value[1] = '69'
key_value[4] = '23'
key_value[5] = '65'
key_value[6] = '34'
key_value[3] = '76'

Output:
(1, 69) (2, 64) (3, 76) (4, 23) (5, 65) (6, 34)
  1. 程序:

Python3

# function calling
def dictionairy():
 
 # Declaring the hash function     
 key_value ={}   
  
# Initialize value
 key_value[2] = 56      
 key_value[1] = 2
 key_value[5] = 12
 key_value[4] = 24
 key_value[6] = 18     
 key_value[3] = 323
  
 print ("Task 2:-\nKeys and Values sorted in",
            "alphabetical order by the key  ") 
 
 # sorted(key_value) returns an iterator over the
 # Dictionary’s value sorted in keys. 
 for i in sorted (key_value) :
    print ((i, key_value[i]), end =" ")
 
def main():
    # function calling
    dictionairy()            
     
# main function calling
if __name__=="__main__":    
    main()
输出:
Task 2:-
Keys and Values sorted in alphabetical order by the key  
(1, 2) (2, 56) (3, 323) (4, 24) (5, 12) (6, 18)

  1. 使用值按字母顺序对键和值进行排序
    例子:
Input:
key_value[2] = '64'       
key_value[1] = '69'
key_value[4] = '23'
key_value[5] = '65'
key_value[6] = '34'
key_value[3] = '76'

Output:
(4, 23), (6, 34), (2, 64), (5, 65), (1, 69), (3, 76)
  1. 程序:

Python3

# Function calling
def dictionairy():
 
 # Declaring hash function     
 key_value ={}   
  
# Initializing the value
 key_value[2] = 56      
 key_value[1] = 2
 key_value[5] = 12
 key_value[4] = 24
 key_value[6] = 18     
 key_value[3] = 323
  
 
 print ("Task 3:-\nKeys and Values sorted",
   "in alphabetical order by the value")
  
 # Note that it will sort in lexicographical order
 # For mathematical way, change it to float
 print(sorted(key_value.items(), key =
             lambda kv:(kv[1], kv[0])))   
  
def main():
    # function calling
    dictionairy()           
     
# main function calling
if __name__=="__main__":     
    main()
输出:
Task 3:-
Keys and Values sorted in alphabetical order by the value
[(1, 2), (5, 12), (6, 18), (4, 24), (2, 56), (3, 323)]

按键排序字典
注意:它将按字典顺序排序
将键的类型作为字符串
程序:

Python3

# Creates a sorted dictionary (sorted by key)
from collections import OrderedDict
 
dict = {'ravi':'10','rajnish':'9','sanjeev':'15','yash':'2','suraj':'32'}
dict1 = OrderedDict(sorted(dict.items()))
print(dict1)

在这里,输出是使用字典顺序的键排序字典
词典顺序: https://en.wikipedia.org/wiki/Lexicographical_order
学习成果:

  • 如何处理字典。
  • Dictionary 的搜索时间复杂度为 O(1),而 List 的搜索时间复杂度为 O(n)。因此,建议尽可能使用字典。
  • 字典的最佳应用可以在Twitter 情绪分析中看到,其中使用 Lexicon 方法分析 Twitter 情绪。