key in adict. keys (): print key2. traverse the dictionary value (value)Copy codeThe Code is as follows:For value in adict. values (): print value3. Traverse dictionary items (elements)Copy codeThe Code is as follows:For item in adict. items (): print item4. traverse the dictionary's key-valueCopy codeThe Code is as follows:For item, value in adict. items (): print 'key = % s, value = % s' % (item, value)
') ' Bob ' >>> Adict.get (' Email ', ' Not found ') ' not found ' Dict.items (): Returns a list containing the dictionary (key, Value) to the tuple Dict.keys (): Returns a list containing the keys in the dictionary>>> Adict.keys ()[' QQ ', ' age ', ' name ']Dict.values (): Returns a list that contains all the values in the dictionary>>> adict.values (
There are three built-in data types in Python. Dictionary (dictionary), list, and tuple (tuples).Meta-group (tuple)Read-only list (the value of the list can be modified, not the Tuple)Tuples are similar to lists, except that elements of tuples cannot be modified.Tuples use parentheses, and the list uses square Brackets.Tuple creation is simple, just add elements
A comprehensive understanding of python strings and dictionaries, and a comprehensive understanding of python
Many serial method strings are also applicable,However, the string is immutable, so some methods that try to change the string are not available.
1 string formatting
1) format the string with tuples or dictionaries
#!/usr/bin/env python
#-*-coding:utf-8-*-"" "
The old rules of the following method environment 2.7.x, please 3.x above version of the Friends remember format print (output in parentheses)
The basic composition and usage of a dictionary
Dict = {Key:value}
dict[Key] = value
First of all, the dictionary is composed of key key and value one b
Basic Python series ---- dictionaries, basic statements, python ----
1. Definition
Ing: a data structure referenced by a name. Different from the data structure mentioned above, one is through the number of the lower mark, and the other is through the name.
A dictionary is the only built-in ing type in
'}
Dict.update (DICT2)Add the key value pairs of the dictionary dict2 to the Dict1, that is, merge Dict1 and dict2 into a dictionary.
dic1 = { 'name' : 'nanliangrexue','age':18}dic2 = { 'gender' : 'male'}dic1.update(dic2)print(dic1)#输出{'name': 'nanliangrexue', 'age': 18, 'gender': 'male'}
Dict.values ()Return all values in a dictionary as a
the dictionary dict
6
radiansdict.items () Returns an array of traversed (key, value) tuples in a list
7
Radia Nsdict.keys () Returns a dictionary of all keys in a list
8
radiansdict.setdefault (Key, Default=none) and get ( ) similar, but if the key does not already exist in the dictionary
A python dictionary is another mutable container model (unordered), and can store any type of object, such as strings, numbers, tuples, and other container models. This article mainly introduces the Python dictionary (Dict) The detailed operation method, contains the creation, the access, the deletion, the other operat
Python learning Note 4-dictionary and collection, and python learning note dictionary
The dictionary is the only ing type in Python. The so-called ing refers to the sequence of the Data Type that contains the hash value (key) and
Examples of operations related to Python strings and dictionaries, and python strings
Examples of operations related to Python strings and dictionaries
String operation:
% Formatting of string:
str = "Hello,%s.%s enough for ya ?"values = ('world','hot')print str % values
Out
This article describes how to sort dictionaries in python. it is a very practical technique and has detailed annotations in the instance, which is easy to understand, for more information about how to sort dictionaries in python, see the following example. Share it with you for your reference.
The specific implement
Python learning-Python container (list, tuples, Dictionary, set), python dictionary
List: The list is ideal for locating an element by order and position, especially when the order or content of the element changes frequently. Unlike strings, the list is variable.You can di
Python's 6 basic data types--dictionariesDictionarydefinition and characteristics of dictionariesA dictionary is the only type of mapping in the Python language. definition : {key1:value1,key2:value2}
1、
键与值用冒号":"分开;
2、项与项用逗号","分开;
features :
1.key:value结构
2.key必须可hash、且必须为不可变数据类型、必须唯一(数字、字符串、元组)
3.value可存放任意多个值、可修改、可以不唯一
4.无序
key is ' 2 ' of 1 pairs of dictionaries Dict.popitem () # Random Delete no eggs with dict.get (' 1 ') # Safe to get value there is a return without a noneprint (' 1 ' in The cycle of a dictionaryFor i in Dict: print (i,dict[i]) # loop his key I corresponding to the dict[i] corresponding value6. Ternary operatorsA = 1b = 2c = 3d = A If a>b else C # has a value equal to if a is greater than B assigns a to D otherwise C assigns a value to DThe nex
, because the default value is set, so the program does not error input Name:phone number (p) or address (a)? ' S is not available. 2. formatting strings for dictionaries(1). After the% in each conversion specifier, you can add the key (enclosed in parentheses), followed by the other explanatory elements.>>> phonebook = {' Wang ': ' 1234 ', ' Ni ': ' 2345 ', ' Ma ': ' 3456 '}>>> "Wang ' s phone number is% (Wang) s." Phonebook "Wang ' s phone number
Comparison of common methods and efficiency for merging two dictionaries in Python, and merging in python
This article describes the common methods and Efficiency Comparison of combining two dictionaries in Python. Share it with you for your reference. The specific analysis
(B, type (B) # run the result {'D': 4, 'B': 2, 'A ': 1} ('C', 3)
10) setdefault (self, k, d = None ):
Obtain the key-k value from the dictionary. if the dictionary contains the key-k value, the function is basically the same as that of get. if the dictionary does not contain the key-k value, add an initial key-value pair with k in the original
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.