The traversal and sort __python of Python dictionaries

Source: Internet
Author: User

The traversal of a dictionary:

First of all:

Items ():

Function: Returns the dictionary key value pair in the form of a list

eg

Dict_={"A": 2, "B": 3, "C": 6}

Dict_.items ()

>>>[(' A ', 2), (' B ', 3), (' C ', 6)]

Iteritems ():

Function: Returns a dictionary key value pair with an iterator object

#-*-coding:cp936-*-
Dict1={' A ': 1, ' B ': 2, ' C ': 3}
#第一种:
For D in Dict1:
Print "%s:%d"% (D,dict1[d])
Print


#第二种:
For k,v in Dict1.items ():
Print "%s:%d"% (k,v)
Print


#第三种:
For k,v in Dict1.iteritems ():
Print "%s:%d"% (k,v)
Print


#第四种:
For k in Dict1.iterkeys ():
Print "%s:%d"% (K,dict1[k])
Print


#第五种:
For V. in Dict1.itervalues ():
Print V
Print


#第六种:
For k,v in Zip (Dict1.iterkeys (), Dict1.itervalues ()):
Print "%s:%d"% (k,v)

Print


The zip () function can merge the list and create a list of meta pairs.

eg

list1=[1,2,3]

list2=[4,5,6]

Zip (a,b)

>>>[(1,4), (2,5), (3,6)]

The zip () function parameter can be any type of sequence, or can have more than two parameters, when the length of the incoming parameter is not the same, the zip automatically to the shortest sequence length of the Intercept, get the Yuan zu.



Sort of Dictionary:

First of all:

function sorted (dic,value,reverse)

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

DIC is the comparison function, and value is the Sort object (key or key value)

Reverse note ascending sort or descending sort with true-descending and false-ascending (default)


Eg: Sort by the key value of the dictionary (replace dict[1] with dict[0] is sorted by the key of the dictionary)

Sorted (Dict.iteritems (), Key=lambda dict:dict[1],reverse=true)

EXPLANATION Note:

Dict.iteritems () Get [(Key, key value), (key, key value), (key, key value) ...] The list. The sorted method is then used to specify that the sort is sorted by the value of the key value, which is the first element d[1], through the key parameter. Reverse=true indicates that you want to flip (that is, descending sort), and the default is ascending sort.


function lambda and function iteritems ()

Lambda:

Functions: Creating 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 a dictionary key value pair with an iterator object


#-*-coding:cp936-*-
Print "Sort by dictionary key value"
Dict1={' A ': 3, ' C ': 1, ' B ': 2}
#升序:
Dict_a=sorted (Dict1.iteritems (), Key=lambda Dict1:dict1[1],reverse=false)
#降序排序reverse =true, this parameter can be saved, and the default is False. or Dict_a.reverse ()

Print dict_a, "\ n" #降序:
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}
#降序:
Dict_c=sorted (Dict3.iteritems (), Key=lambda dict3:dict3[0],reverse=true)
#降序排序reverse =true, this parameter can be saved, and the default is False. or Dict_a.reverse ()

Print Dict_c, "\ n" #升序:
dict4={' d ': 6, ' E ': 5, ' F ': 4}
Dict_d=sorted (Dict4.iteritems (), Key=lambda dict4:dict4[0]) #改为降序与上面同理
Print Dict_d, "\ n"


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.