Common data structures in Python can be collectively referred to as Containers (container). sequences (such as lists and tuples), mappings (such as dictionaries), and collections (set) are the three main types of containers.
One, sequence (list, tuple, and string)
Each element in the sequence has its own number. There are 6 types of built-in sequences in Python. where lists and tuples are the most common types. Other include strings, Unicode strings, buffer objects, and Xrange objects. The following tables, tuples, and strings are highlighted below.
1. General sequence Operation:
Some common common methods that can "abstract" out sequences from lists, tuples, and strings (not the crud you imagine) are: indexes (indexing), shards (sliceing), plus (adding), Multiply (multiplying) and check whether an element belongs to a member of a sequence. In addition, there are built-in functions such as calculating the length of the sequence and the maximum minimum element
1) Index:
1 str1='Hello'2 nums=[1,2,3,4]3 t1= (123,234,345 )4print str1[0]5print nums[1]6 print t1[2]
Output:
H
2
345
2) Shard:
A shard operation is used to access an element within a certain range. Shards are implemented by two indexes separated by a colon:
1Nums=range (10)2 PrintNums3 PrintNums[1:5]4 PrintNums[6:10]5 PrintNums[1:]6 PrintNums[-3:-1]7 PrintNums[-3:]#include the element at the end of the sequence, empty the last index8 Printnums[:]#Copy entire sequence
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4]
[6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[7, 8]
[7, 8, 9]
Different steps, with different outputs:
1Nums=range (10)2 PrintNums3 PrintNUMS[0:10]#The default step size is 1 equivalent to nums[1:5:1]4 PrintNums[0:10:2]#Step size is 25 PrintNums[0:10:3]#Step size is 36 ##print nums[0:10:0] #步长为07 PrintNums[0:10:-2]#step is-2
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]
[0, 3, 6, 9]
[]
3) Add the sequence:
Two sequences of the same type can be added;
1 str1='Hello'2 str2=' World'3 print str1+str24 num1=[1,2,3]5 num2=[2,3,4]6 print num1+num27print str1+num1
Output:
Hello World
[1, 2, 3, 2, 3, 4]
Traceback (most recent):
File "F:\Python\test.py", line 7, <module>
Print Str1+num1
Typeerror:cannot concatenate ' str ' and ' list ' objects
4) Multiplication:
1 Print [none]*102 str1='Hello'3print str1*24 num1=[1,2 ]5print num1*26print str1*num1
Output:
[None, None, none, none, none, none, none, none, none, none]
Hellohello
[1, 2, 1, 2]
Traceback (most recent):
File "F:\Python\test.py", line 5, <module>
Print Str1*num1
Typeerror:can ' t multiply sequence by non-int of type ' list '
5) Membership:
The in operator is used to check whether an object is a member (that is, an element) of a sequence (or other type):
1 str1='Hello'2print'h'in 3 print ' H ' in str1 4 num1=[1,2]5print in NUM1
Output:
False
True
True
6) Length, minimum and maximum values:
1 str1= " hello 2 Len (str1) 3 print Max (str1) 4 print min (str1) 5 num1=[1,2,1,4,123] 6 Len (NUM1) 7 print Max (NUM1) 8 print min (NUM1)
Output:
5
O
H
5
123
1
2. List
The list is mutable, which is the most important feature that distinguishes it from strings and tuples, in a nutshell: Lists can be modified, and strings and tuples cannot.
1 list1=['hello','World']2 Print List1 3 list2=[1,2,3]4print list2
Output:
[' Hello ', ' world ']
[1, 2, 3]
1) List function:
It is very useful to create a list of strings through the list function (in fact, list is a type rather than a function):
1 list3=list ("hello")2print list3
Output:
[' H ', ' e ', ' l ', ' l ', ' O ']
Join function:
The implementation converts a list of characters into a string;
1 somelist = ['a','b','c' ]2 list1 = ". Join (somelist)3print list
Output:
' ABC '
2) Basic list operation:
Element Assignment Value:
1 x = [1,1,1]2 x[1] = 23print x
Output:
[1,2,1]
To delete an element:
1 list1 = ['h','e','l', ' L ','o']2del list1[1] 3 Print List1
Output:
[' H ', ' l ', ' l ', ' O ']
Shard Assignment:
1 name = List ('Perl')2 name[1:] = list ('Ython ')3print name
Output:
[' P ', ' y ', ' t ', ' h ', ' o ', ' n ']
3) List method:
Append method:,
Appends a new object to the end of the list;
1 list1 = [2] list1.append (5)3print List1
Output:
[1,2,3,5]
Count Method:
Count the number of occurrences of an element in the list;
result = ['to','is','or',' not ','to','being' ]print result.count ('to')
Output:
2
Extend method:
The Extend method changes the original list, and the connection operation (' + ') does not change the original list;
Python data structure detailed