(Info.values ()) #打印所有的value执行结果: Dict_values ([' Li si ', ' Zhang san ', ' Wang Wu ']) (2) Keysprint (Info.keys ()) #打印所有的key执行结果: Dict_keys ([' stu1103 ', ' stu1102 ', ' stu1101 ']) (3) SetDefault the value in the dictionary, if it can be returned directly, if the value cannot be taken, the creation will increase. Info.setdefault ("stu1106", "Alex") #增加键值对print (info) execution result: {' stu1102 ': ' Li
In Python, metadata groups, lists, Dictionary differences, and python dictionaries
Python has three built-in data structures: List, tuples, and dictionary.1. ListList is the data structure that processes a group of ordered projects. You can store a series project in a list.
Data structures that refer to values by name in Python are called mappings (mapping). Dictionaries are the only types of mappings in Python that are built in (the Python interpreter itself supports, and does not require import). The values in the dictionary are not in a special order and are stored under a specific key
Common definition methods for Python dictionaries
Define a dictionary directlyDict1 = {' x ': 1, ' Y ': 2, ' Z ': 3}
Define a dictionary using the Dict methodDict2 = Dict (X=1, y=2, z=3)
Define a dictionary using
string into a sequence.
print ' 1+2+3+4+5 '. Split (' + ') output: [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
(6) Strip method: Remove both sides of the string, the default is a space string, you can also specify the corresponding string.
PS: You can also participate in Lstrip and Rstrip methods.
(7) Translate method: As with the Replace method, some parts of the str
dictionary built-in functions methodsThe Python dictionary contains the following built-in functions:
Serial Number
functions and descriptions
1
CMP (Dict1, DICT2)Compares two dictionary elements.
2
Len (dict)Calcula
Python dictionary is a very important base type in Python, it is an efficient base type, not only used in writing programs, but even the implementation of Python's low-level use of a large number of dictionaries.
Learn Python dictionaries mainly from a few aspects
1. Basic use of Dictionaries
index position of 2 in the string data that a points to>>> a= "ABCDEFG">>> print (a[2])C
#在列表数据中, you can think of each string element as a child list, using the idea of multidimensional lists to extract values from the corresponding index in the child list>>> b=["www.qingsword.com", "ABCDEFG", "12345678"]>>> print (b[0][4])Q
#提取b中索引位置为2的元素的最后一位字符>>> print (b[
) the items () function: Gets all the items of a function
For Eachitem in Dict1.items (): print (eachitem,end= "")
(5) The Get () function gets the value corresponding to the dictionary key and returns none if it does not exist
Str1=dict1.get (+) print (STR1)
(6) In or not in member relations: Judging whether a key or value is in the dictionary
Print ("\ n") print ("\ n") print ("\ Dict1")
(
Calculations related to dictionary values
Problem
You want to calculate the value of a dictionary, for example, to find the item with the largest (smallest) corresponding value in the dictionary.
Solution One:
Suppose you want to find the item with the smallest value from the dictionary {' A ': 3, ' B ':
Python lexicographically problematic instances and python dictionary instances
This article describes the python lexicographic issue and shares it with you for your reference. The details are as follows:
Problem description:
The order of letters from left to right is the same as that in the alphabet, and each character
The dictionary is the only type of mapping in python, its hash value (key Key) and the point to object (value) is a one-to-many relationship;A dictionary is a mutable object, is a container type, can store any number, any type of mapping type, and is enclosed in curly braces ({key1:value, key2:value2, ...). });The data types in the
returned values can contain duplicates.
Create
Method One:
The code is as follows
Copy Code
>>> Dict1 = {}>>> dict2 = {' name ': ' Earth ', ' Port ': 80}>>> Dict1, Dict2({}, {' Port ': ' Name ': ' Earth '})
Method Two: From the Python version 2.2, you can use a factory method to pass in an element that is a tuple of a list as an argument
The code is as follows
Copy Code
>>> fdict =
contents of the list, enter the B,\N3: Enter the module please input Q,\N4: Return to the upper level Please enter D") if choice.lower () = = "C": Chazh AO = input ("Please enter the name of the list to view") if Chazhao in Diejia:key_case.append (Chazhao) Else:print (" The list name does not exist ") elif choice.lower () = =" B ": Tianjia = input (" Please enter what you want to add to this list ") if Tianjia in Diejia: Print ("The list name already exists") Else:diejia[t
Python dictionaryis an unordered, data type stored in key-value pairs , with strong data affinity and a unique mapping data type. key: Must be a hash (immutable data type: string, number, tuple, bool) value and is uniqueNone: None is an empty object, ' is a string object, none is a special data typeDictionary method:dic={"Name" = ' Alex ', ' sex ' = ' men '} up = {' name ': ' Ritian ', ' sex ': ' Men ', ' job ': ' It '}Increase:(1) The first method: D
relational data.The dictionary is unordered before Python 3.5 contains 3.5, but is ordered after 3.6.2, the dictionary additions and deletions and other operationsThe key is unique when the same key is present in the back of the overlay front. For example:DIC = { ' name ': ' Alex ', ' name ': ' Wusir '}print (DIC)The key must be a data type that is
The dictionary is the only built-in mapping type in Python. The values in the dictionary are not in a particular order, but are stored in a particular key.
A key can be a number, a string, or even a tuple.
1. Creating and Using dictionaries
Dictionaries can be created in the following ways:
Copy Code code as follows:
Phonebook = {' Alice ': ' 2341
The Python dictionary and JSON are very similar in form. In fact, JSON is actually a string representation of the Python dictionary. However, as a complex object, the dictionary cannot be directly converted into a string defining its code, let's analyze it in detail # This i
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.