First, Python has a lot of built-in data structures, which are categorized from different angles.
1. Single value and set
Once the data structure of a single-valued type cannot be changed , if it is not referenced, it is reclaimed by the system, and if you want to change a single-valued type of the schema, Python is creating a new data structure.
Data structures of a single value type are stored continuously in memory.
A=1print(ID (a))#2006314800a=2 print(ID (a )) #2006314832 #尝试改变数字a, Discovery opens up new memory space
The data structures of a collection type are composite, such as
1A=[1,'2','string']2 #a list consisting of numbers and strings3b= (1,'string')4 #tuples made up of numbers and strings5 6c=[['1','2','string'],'Zhoufeng']7 #a list of lists and strings8 9D={1: (1,'string'), 2: (2,'string')}Ten #dictionaries consisting of numbers and tuples
2, according to whether the orderly division
"Orderly" is a very important feature, there are many operations around the "orderly" expansion, the following list of common methods of built-in data structures.
Second, the data structure method
Add: The difference between methods and functions is that the method is bound to a class instance (object), because a data structure is also an object, so the data structure has a method, the concept of a specific class is described later.
1. Digital operation
2. Sequence generic operation (for lists, tuples, strings)
① Index
greeting='hello'
NUMBERS=[1,2,3,4,5,6,7,8,9,10]
Print# index starting from 0
#h
Print (Greeting[-1]) # -1 means the countdown is the first one, so it's better to remember .
# o
② shards
Print (Greeting[2:4]) #第1个索引是要提取的第1个元素的编号, the 2nd index is the number of the 1th element of the remainder of the Shard, in short, "Gubai"
#11
Print (numbers[-3:]) #取倒数3个元素
#[8, 9, 10]
Print (Numbers[:3]) #取前3个元素
#[1, 2, 3]
Print (Numbers[0:10:2]) #设置步长为2, the stride size can be negative, which means extracting elements from right to left
#[1, 3, 5, 7, 9]
③ sequence Addition
#两种相同类型的序列才能够相加
Print (' Hello, ' + ' world ')
#hello, World
Print ([1,2,3]+[4,5,6])
[1, 2, 3, 4, 5, 6]
④ Sequence Multiplication
Print ([1,2]*5)
#[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
Print (' Hello ')
Hellohellohellohellohello
⑤ Membership #存在性检查
Print (' h ' in ' Hello ')
True
Print (1 in [+/-)]
True
Print (' Zhou ' in (' Zhou ', ' Feng '))
True
⑥Max, Min, Len
Print (max ([+])) #如果是数字序列, it is the comparison value size
Print (max ([' Zhou ', ' Feng ', ' de ')) #如果是字符, it is more ASCII size
Print (Len ([' Zhou ', ' Feng ', ' de ', ' shu ')]) #返回序列中元素个数
#4
⑦ Traversal (iteration) #列表和元组按元素遍历, string traversal by character
lst1=[' item1 ', ' item2 ', ' item3 ', ' item4 ']
For I in Lst1:
if i = = ' item2 ':
Print (' Pass ')
Continue
if i = = ' Item3 ':
Print (' found! ')
Break
#pass
#found!
3. List operation
The list is essentially a chain of tables, which does not require space to be contiguous, which is why it can be modified.
① listfunction #将任意一个序列 (list, tuple, string)
A=list ("Hello")
Print (a)
#[' h ', ' e ', ' l ', ' l ', ' O ']
② element Assignment value
x=[1,1,1]
x[1]=2
Print (x)
#[1, 2, 1]
Delete Element
names=[' Alice ', ' Beth ', ' Cecil ', ' Dee-dee ']
Del Names[2]
Print (names)
#[' Alice ', ' Beth ', ' Dee-dee '
④ Shard Assignment
d=[1,2,3,4]
d[-2:]=[5,6]
Print (d)
⑤Append #在末尾追加新的Object , note that the modified copy is not generated in situ
list1=[1,2]
list2=[3,4]
List1.append (LIST2)
Print (List1)
#[1, 2, [3, 4]]
⑥Extend #扩展, append multiple values of another sequence at the end of the list, Note that it is modified in place, and that no modified copy is generated, which is different from the sequence addition.
list3=[1,2]
list4=[3,4]
List3.extend (LIST4)
Print (LIST3)
#[1, 2, 3, 4]
⑦Index #通过值取索引, and the process by which values are indexed is reversed
list5=[' we ', ' is ', ' winner '
Print (List5.index (' is '))
#1
⑧Insert #在指定位置插入元素
list6=[1,2,3,4]
List6.insert (2, ' item ')
Print (LIST6)
#[1, 2, ' Item ', 3, 4]
⑨pop #弹出一个元素, If no subscript is specified, the last one by default
list7=[' item1 ', ' item2 ', ' item3 ', ' item4 ']
Print (List7.pop ())
#item4
list8=[' item1 ', ' item2 ', ' item3 ', ' item4 ']
Print (List8.pop (0))
#item1
⑩Remove #按值删除元素, if there is more than one value, delete the first occurrence
List9=[' my ', ' name ', ' is ', ' Zhoufeng ', ' Zhoufeng ', ' is ', ' me ']
List9.remove (' Zhoufeng ')
Print (LIST9)
#[' my ', ' name ', ' is ', ' Zhoufeng ', ' is ', ' me ']
? Sort #排序, sort in place , do not generate a sorted copy, if you want not to change the original list, but instead generate a sorted copy, you can use sorted ()
List1=[' A ', ' s ', ' d ', ' f ', ' g ', ' X ']
List1.sort ()
Print (List1)
#[' A ', ' d ', ' f ', ' G ', ' s ', ' X ']
4, tuple operation
①tuple () function #将任意的序列变为一个元组
#元组存在的意义:
The ① can be used as a mapped key, and the list cannot be
② tuples exist as return values for many of the built-in functions and methods
5. String manipulation
① String Formatting
Str1= '%s plus%s equals%s '% (1,1,2) # Note the string in quotation marks, the formatted object cannot be in quotation marks, the last% is the format operator, and the preceding% is the placeholder
Print (STR1)
#1 plus 1 equals 2
Print ('%s plus%s equals%s '% (1,1,2))
#1 plus 1 equals 2
②Find #返回子串的索引
Str1= "This is a test string"
Print (Str1.find (' is '))
#2
③jion #将序列中的元素通过指定的符号连接成一个字符串 #注意序列中的元素必须是字符串
list1=[' 1 ', ' 2 ', ' 3 ', ' 4 ']
Print (' + '. Join (List1)) #为什么不是list1. Join (' + ')?
#1 +2+3+4
④ Lower #将字符串中的大写变为小写, Note that the newly generated string is not returned , and print (Str1.lower ()) is wrong.
str1= "HELLO World"
Str1=str1.lower ()
#hello World
⑤Replace #替换所有匹配的子串 to return the newly generated string .
Str1= ' This is a test '
Print (Str1.replace (' is ', ' EEZ '))
#theez EEZ a test
⑥split #按给定的分隔符切割字符串, and join is the opposite process
⑦strip #去掉开头和结尾的空格, and returns the newly generated string
str2= ' Hello World '
Print (Str2.strip ())
#hello World
6. Dictionary operation
#字典是python中the only built-in mappings , by key values, keys can be numbers, strings, tuples
#字典是无序的, the For loop iterates through the elements and is output out of order.
① take all the keys, take all the values, take all the elements
dict1={' name ': ' Zhoufeng ', ' sex ': ' Man ', ' age ': ' 26 '}
Print (Dict1.keys ()) #以字符串列表形式输出所有的键
#dict_keys ([' Age ', ' sex ', ' name '])
Print (dict1.values ()) #以字符串列表形式输出所有的值
#dict_values ([' + ', ' man ', ' Zhoufeng '])
Print (dict1.items ()) #以元组列表的形式输出所有的元素, only for the For Loop
#dict_items ([' Age ', ' + '), (' Sex ', ' man '), (' Name ', ' Zhoufeng ')])
② Traversal Dictionary
dict1={' name ': ' Zhoufeng ', ' sex ': ' Man ', ' age ': ' 26 '}
For Key,value in Dict1.items ():
Print (Key,value)
Print ("==============")
#sex Mans
#==============
#name Zhoufeng
#==============
#age 26
#==============
Python basic Knowledge---data structure