The content of this section
1. List, tuple operations
2. String manipulation
3. Dictionary operation
Python Three mesh operations
>>> A, B =
>>> A
1
>>> b
2
>>> C=a if a>b else b
>>> C
2
Enumerate #函数用于遍历序列中的元素以及它们的下标
names = [' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG ']
For Index,name in Enumerate (names):
Print (Index,name)
(0, ' AA ')
(1, ' BB ')
(2, ' CC ')
(3, ' DD ')
(4, ' EE ')
(5, ' FF ')
(6, ' GG ')
1. List, tuple operations
The list is one of the most commonly used data types in the future, and the list allows for the most convenient storage, modification and other operations of the data.
Definition List
names = [' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' FF ']
To access the elements in the list by subscript, the index is counted starting at 0
>>> Names[0]
' AA '
>>> Names[1]
' BB '
>>> Names[-1] <---can also be taken backwards.
' FF '
>>> Names[-2]
' EE '
Slicing: Taking multiple elements (the general principle is Gu Tou regardless of the tail)
>>> Names[1:4] #取下标1至下标4之间的数字, including 1, not including 4
[' BB ', ' cc ', ' DD ']
>>> Names[1:-1] #取下标1至-1 value, excluding-1
[' BB ', ' cc ', ' dd ', ' EE ']
>>> Names[0:3]
[' AA ', ' BB ', ' CC ']
>>> Names[:3]
[' AA ', ' BB ', ' cc '] #如果是从头开始取, 0 can be ignored, the same as the effect of the sentence
>>> Names[3:]
[' DD ', ' ee ', ' FF '] #如果想取最后一个, must not be written-1, can only be written like this
NAMES[0::2]
[' AA ', ' cc ', ' ee '] #后面的2是代表, every other element, take a
>>> Names[::2]
[' AA ', ' cc ', ' ee '] #和上句效果一样
Additional
>>> names = [' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' FF ']
>>> names
[' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' FF ']
>>> names.append (' GG ')
>>> names
[' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG ']
Insert
>>> names
[' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG ']
>>> Names.insert (2, ' hh ')
>>> names
[' AA ', ' BB ', ' hh ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG '] #强行从cc前面插入
Modify
>>> names
[' AA ', ' BB ', ' hh ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG ']
>>> names[2]= (' AB ')
>>> names
[' AA ', ' BB ', ' AB ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG ']
Delete
>>> names
[' AA ', ' BB ', ' AB ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG ']
>>> del Names[2]
>>> names
[' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' GG ']
>>> names.remove (' GG ') #指定删除gg
>>> names
[' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' FF ']
>>> Names.pop ()
' FF '
>>> names #删除最后一个值
[' AA ', ' BB ', ' cc ', ' dd ', ' EE ']
Extended
>>> names
[' AA ', ' BB ', ' cc ', ' dd ', ' EE ']
>>> B = [+ +]
>>> Names.extend (b)
>>> names
[' AA ', ' BB ', ' cc ', ' dd ', ' ee ', 1, 2, 3]
Copy
>>> names = [' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' FF ']
>>> name=copy.copy (names)
>>> Name
[' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' FF ']
Statistics
>>> names = [' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' AA ', ' FF ']
>>> names.count (' AA ')
2
Sort & Invert
>>> names = [' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' AA ', ' ff ', ' a ')
>>> Names.sort ()
>>> names
[1, 2, 3, ' AA ', ' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' FF ']
>>> names = [1, 2, 3, ' AA ', ' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' FF ']
>>> Names.reverse ()
>>> names
[' FF ', ' ff ', ' ee ', ' dd ', ' cc ', ' BB ', ' AA ', ' AA ', 3, 2, 1]
Get subscript
>>> names = [1, 2, 3, ' AA ', ' AA ', ' BB ', ' cc ', ' dd ', ' ee ', ' ff ', ' FF ']
>>> names.index (' AA ')
3 #只返回找到的第一个下标
Loop List
>>> for I in Names:
... print (i)
...
Aa
Bb
Cc
Dd
Ee
Ff
Meta-group
Tuples are actually similar to the list, but also to save a group of numbers, it is not once created, it can not be modified, so it is called a read-only list
Grammar
Names = ("AA", "BB", "CC")
It has only 2 methods, one is count, the other is index
2. String manipulation
3. Dictionary operation
A key-value data type, used as a dictionary of our school, to check the details of the corresponding page by strokes and letters.
Grammar:
>>> info={' age1 ': Ten, ' Age2 ':, ' Age3 ': 30}
Dictionary features: Dict is unordered, key must be unique and inherently heavy
Increase
info[' Age5 ']=50
>>> Info
{' Age5 ': +, ' age4 ': +, ' age3 ': +, ' age2 ': +, ' age1 ': 10}
Modify
>>> info[' Age4 ']=60
>>> Info
{' Age5 ': ' age4 ': ' Age3 ': ', ' age2 ': +, ' age1 ': 10}
Delete
>>> info.pop (' age4 ') #标准删除
60
>>> Info
{' Age5 ': +, ' age3 ': +, ' age2 ': +, ' age1 ': 10}
>>> del info[' Age5 '] #换个姿势删除
>>> Info
{' Age3 ': +, ' age2 ': $, ' age1 ': 10}
Info
{' Age3 ': +, ' age2 ': $, ' age1 ': 10}
>>> Info.popitem () #随机删除
(' Age3 ', 30)
>>> Info
{' Age2 ': $, ' age1 ': 10}
Find
>>> info={' age1 ': Ten, ' Age2 ':, ' Age3 ': 30}
>>> ' age1 ' in info #标准用法
True
>>> info.get (' Age1 ') #获取
10
>>> info[' Age1 '] #同上
10
>>> info[' age4 '] #如果一个key不存在, error, get no, no only return none
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror: ' Age4 '
Cyclic dict
>>> for key in info:
... print (Key,info[key])
...
(' Age3 ', 30)
(' Age2 ', 20)
(' Age1 ', 10)
Python basic operations