Python implements the dictionary traversal and sorting function example

  • 2020-06-19 10:50:24
  • OfStack

An example of Python to implement the dictionary traversal and sorting function. To share for your reference, specific as follows:

Dictionary traversal:

First of all:

items () :

Function: Returns dictionary key - value pairs as a list

eg:


dict_={"a":2,"b":3,"c":6}
dict_.items()
>>>[('a',2),('b',3),('c',6)]

iteritems () :

Function: Returns dictionary key - value pairs as iterator objects


# -*- coding: cp936 -*-
dict1={'a':1,'b':2,'c':3}
# The first 1 A: 
for d in dict1:
  print "%s:%d"%(d,dict1[d])
print
# The first 2 A: 
for k,v in dict1.items():
  print "%s:%d"%(k,v)
print
# The first 3 A: 
for k,v in dict1.iteritems():
  print "%s:%d"%(k,v)
print
# The first 4 A: 
for k in dict1.iterkeys():
  print "%s:%d"%(k,dict1[k])
print
# The first 5 A: 
for v in dict1.itervalues():
  print v
print
# The first 6 A: 
for k,v in zip(dict1.iterkeys(),dict1.itervalues()):
  print "%s:%d"%(k,v)
print

zip() The function merges lists and creates a list of pairs of primitives.

eg:


list1=[1,2,3]
list2=[4,5,6]
zip(a,b)
>>>[(1,4),(2,5),(3,6)]

zip() Function parameters can be any type of sequence or have more than two parameters. When the length of parameters is different, zip automatically intercepts with the shortest sequence length to obtain the meta ancestor.

The sorting of dictionaries:

First of all:

function sorted(dic,value,reverse)

Procedure: The first parameter is passed to the second parameter "key-key value", and the second parameter takes out the key [0] or key value [1].

dic is the comparison function, value is the sort object (key or key value)

reverse indicating ascending or descending, es48EN-descending and false-ascending (default)

eg: Sort by dictionary key (replace dict[1] with dict[0] to sort by dictionary key)


sorted(dict.iteritems(),key=lambda dict:dict[1],reverse=True)

Explanation:

dict.iteritems() Get [(key, key value),(key, key value),(key, key value)...] In the list. The sorted method is then used to specify that the sorting is by the key value, the value of the first element, d[1]. reverse=True means that you want to flip (that is, descending sort), and the default is ascending sort.

Functions lambda and functions iteritems()

lambda:

Functionality: Create anonymous functions

eg:


fun_1=lambda a:a+1
print fun_1(1)
>>>2
fun_2=lambda a,b:a+2*b
fun_2(1,1)
>>>3

iteritems () :

Function: Returns dictionary key - value pairs as iterator objects


# -*- coding: cp936 -*-
print " Sort by dictionary key "
dict1={'a':3,'c':1,'b':2}
# Ascending order: 
dict_a=sorted(dict1.iteritems(),key=lambda dict1:dict1[1],reverse=False) 
# Descending order reverse=True , This parameter can be saved and defaults to False .   or dict_a.reverse()
print dict_a,"\n"
# Descending order: 
dict2={'a':3,'c':1,'b':2}
dict_b=sorted(dict2.iteritems(),key=lambda dict2:dict2[1],reverse=True)
print dict_b,"\n"
##############################################################
print " Sort by dictionary key "
dict3={'d':6,'e':5,'f':4}
# Descending order: 
dict_c=sorted(dict3.iteritems(),key=lambda dict3:dict3[0],reverse=True) 
# Descending order reverse=True , This parameter can be saved and defaults to False .   or dict_a.reverse()
print dict_c,"\n" 
# Ascending order: 
dict4={'d':6,'e':5,'f':4}
dict_d=sorted(dict4.iteritems(),key=lambda dict4:dict4[0])# Descending order is the same as above 
print dict_d,"\n"

PS: Here's another demo tool for sorting:

Online animation demonstration of insert/Select/Bubble/merge/Hill/Quicksort algorithm process tools:
http://tools.ofstack.com/aideddesign/paixu_ys

For more readers interested in Python, please refer to Python Data Structure and Algorithm Tutorial, Python Encryption and Decryption Algorithm and Skills Summary, Python Coding skills Summary, Python Function Use Skills Summary, Python String Manipulation Skills Summary and Python Introductory and Advanced Classic Tutorial.

I hope this article has been helpful in Python programming.


Related articles: