Python Day 1-getting started with common operations on the Python (4) dictionary, python Day 1
1 # dic = {[123, 3]: '000000'} # variable types cannot be used as Dictionary keys. value can be of any type 2 # dic = {(, 4 ): '000000'
Pythondictionary (Dictionary)A dictionary is another mutable container model and can store any type of object.Each key value of the dictionary is key=>value with a colon : Split, with a comma, split between each key-value pair, and the entire dictionary is enclosed in curly braces {}, as shown in the following format:
This article mainly introduces the Python dict dictionary structure operation method learning notebook, the dictionary operation is the basic knowledge in the Python getting started learning, for more information about how to learn about Python dict
My Python self-learning Path 3: tuples and dictionary learning summary, the path to python
1. tuples
1. the Python tuples are similar to the list, except that the elements of the tuples cannot be modified.
The tuples use parentheses, And the list uses square brackets.
Creating tuples is simple. You only need to add ele
. str (dict): Output dictionary printable strings. 4. type (variable): return the input variable type. If the variable is a dictionary, the dictionary type is returned. The Python dictionary contains the following built-in methods: 1. radiansdict. clear (): delete all elemen
Baptism of the soul, cultivation of python (8)-efficient dictionary, baptism of pythonThe core of python-Dictionary (dict)
1. What is a dictionary?
Have you used dictionaries in the student age? You can use a dictionary to find th
First, generate a random dictionary:From random import RandintD={x:randint (60,99) for x in "ABCDEFG"} # {' A ': +, ' B ': $, ' C ': "All, ' d ':", ' e ': ", ' F ': $, ' G ': 88}In the first way, the subclasses of collections are sorted from large to small counter:From collections Import CounterD={x:randint (60,99) for x in "ABCDEFG"}C=counter (d). Most_common () #返回一个列表, in accordance with the value of dict from large to small sort [(' B ', '), (' G ', "), (' d ',"), (' E ', "), (' A '," five
appear twice. If the same key is assigned twice during creation, the last value will be remembered as follows:
The code is as follows:
#! /Usr/bin/pythonDict = {'name': 'Zara ', 'age': 7, 'name': 'manni '};Print "dict ['name']:", dict ['name'];# Output result of the above instance:# Dict ['name']: Manni
2) the key must be unchangeable. Therefore, you can use numbers, strings, or tuples to act as the key.
== 0: return 'zero'elif arg == 1: return 'one'elif arg == 2: return "two"else: return "nothing"
Good
data = { 0: "zero", 1: "one", 2: "two",}data.get(arg, "nothing")
7. Use iteritems to iterate the elements in the dictionary
Python provides several methods to iterate the el
Do you still remember to learn the Python dictionary from old Qi ?, Qi xue python
Dictionary. Are you still using this item? With the development of the network, fewer and fewer users are used. Many people are used to searching on the Internet, not only the web version, but also the various dictionaries of the mobile v
value of the element
7
list.remove (obj) Remove the first occurrence of a value in a list
8
list.reverse () Reverse list elements
9
list.sort ([func]) Sort the original list
tuple ===========================================================A python tuple is similar to a list in that its elements cannot be modified and use
1. array and Variability
When an array is created, it is bound to an object.
L1 = [1, 2, 3] L2 = L1L1[0] = 4 print(L2)#=》[4, 2, 3]
L2 = L1 means that L2 and L1 point to the same object, while L1 [0] = 4 changes the value of the object, so the final L2 value also changes, can be compared with the following example
A = 1 # a points to object 1b = a # B points to object aa = 4 # at this time a points to object
code is as follows:
>>> d={' title ': ' Python Web Site ', ' url ': ' http://www.python.org ', ' spam ': 0}>>> D.items ()[(' url ', ' http://www.python.org '), (' spam ', 0), (' title ', ' Python Web Site ')]
The Iteritems method works roughly the same, but returns an iterator object instead of a list:
The code is as follows:
>>> It=d.iteritems ()>>> it
>>> List (IT)
[(' url ', ' http://www.py
Summary of the basic knowledge of the dictionary in Python, and summary of python
Define a dictionary
>>> d = {"server":"mpilgrim", "database":"master"} 1>>> d{'server': 'mpilgrim', 'database': 'master'}>>> d["server"] 2'mpilgrim'>>> d["database"] 3'master'>>
Python string, list, tuples, set, dictionary method, python stringList
1. L. append (object)-> None
Add a single element at the end of the list. Any type is acceptable, including list or tuples.
2. L. extend (iterable)-> None
Add multiple elements at the end of the list in sequence
3. L. insert (index, object)-> None
example:In [176]: Dict = {' name ': ' Zara ', ' age ': 7, ' name ': ' Manni '};in [177]: Print dict[' name ']manni2) The key must be immutable, so it can be used as a number, a string, or a tuple, so you can't use a list.Dictionary built-in functions methodsThe Python dictionary contains the following built-in functions:
Serial Number
Funct
What is a dictionary?
A dictionary is the only type of mapping in the Python language.
A hash value (key, key) and a pointer to an object (values, value) are a one-to-many relationship that is often considered a mutable hash table in the mapping type object.
The Dictionary object is mutable, which is a container type
Http://btmiller.com/2015/04/13/get-list-of-keys-from-dictionary-in-python-2-and-3.htmlGet a List of Keys from a Dictionary in Both Python 2 and Python 3It was mentioned in an earlier po
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.