程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The creation of dictionary based on Python

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .


One 、 background

In the actual development process , We will encounter situations where we need to correlate relevant data , for example , Deal with student id 、 full name 、 Age 、 Grades and other information . in addition , There will also be situations where different objects that can be identified need to be considered as a whole .Python Two data structures, dictionary and set, are provided to solve the above problems . Here is an introduction python Knowledge about the creation of dictionaries .


Two 、 Dictionary creation

The dictionary is Python A data storage structure commonly used in , It is from “ key - value ” The composition of , Every “ key - value ” Symmetry as one element , Each element represents a mapping or correspondence . “ key ” It can be Python Any immutable data in , Such as integers 、 The set of real Numbers 、 The plural 、 character string 、 Tuples and so on , But you can't use lists 、 aggregate 、 A dictionary or other variable type used as a dictionary “ key ”. “ value ” You can take any data type .

1. Create dictionary by direct assignment

The general format of creating a dictionary by direct assignment is as follows :

Variable name = { key 1: value 1, key 2: value 2, key 3: value 3,…}

for example : Create a student information dictionary , Including student ID 、 Name and gender .

stu_info = {'num':'20180101', 'name':'Liming', 'sex':'male'} # Create a dictionary
print(stu_info) # View dictionary

give the result as follows :

The order in which the elements in the dictionary are printed is not necessarily the same as the order in which they were created , This is because there is no sequence of the elements in dictionary .


2. Use built-in functions dict() Create a dictionary

You can also use built-in functions dict() Through others “ Dictionaries ”、“( key , value )” To create a sequence or keyword parameter of . example : Use built-in functions dict() Create a dictionary .

stu_info2 = dict(stu_info1) # Create... From other dictionaries
stu_info3 = dict([('num', '20180101'), ('name', 'Liming'), ('sex', 'male')]) # adopt “( key , value )” Create a sequence for
stu_info4 = dict(num = '20180101', name = 'Liming', sex = 'male') # Create... With keyword parameters
stu_info5 = dict(zip(['num', 'name', 'sex'], ['20180101', 'Liming', 'male'])) # adopt dict and zip Combined creation
if stu_info1 == stu_info2 == stu_info3 == stu_info4 == stu_info5: # Determine whether the five variables are equal
print(" To create a dictionary 5 In the same way ") # If the same output prompt
else: # If it's not the same
print(" To create a dictionary 5 Different methods ") # Output prompt

give the result as follows :

zip() Function takes an iteratable object as an argument , Package the corresponding elements in the object into tuples , And then it returns the tuple of zip object . for example :

list_str = ['a', 'b', 'c', 'd'] # Create a list and assign values
list_num = [1, 2, 3, 4] # Create a list and assign values
list_new = zip(list_str, list_num) # Packed as tuples zip object
print("zip result ( list ):", list(list_new)) # use list() Function to convert to list output

give the result as follows .


3. Use fromkeys() Method to create a dictionary

stay Python in , When all keys correspond to the same value , You can use fromkeys() Method to create a dictionary .

  • seq For the dictionary “ key ” List of values
  • value Set the key sequence for (seq) Value , When omitted, the default is None

dict.fromkeys(seq[,value]) for example :

stu_age1 = dict.fromkeys(['Wangwu', 'Zhangsan']) # Create a dictionary ,“ key ” The value is defaults to None
print(stu_age1) # Output stu_age1
stu_age2 = dict.fromkeys(['Wangwu', 'Zhangsan'], '18') # Create a dictionary ,“ key ” The value is defaults to 18
print(stu_age2) # Output stu_age2

give the result as follows .

In the dictionary “ key ” Is the only one. , When creating a dictionary, if “ key ” The same thing , Then the defined after “ key - value ” Yes, the previously defined... Will be overwritten “ key - value ” Yes . for example :

x = {'a': 1, 'b': 2, 'b': '3'} # Create dictionary by direct assignment x
print(x) # Output Dictionary x

give the result as follows .


3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


Four 、 summary

The above is about python Knowledge about the creation of dictionaries , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved