List
The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
A = ['spa'egg', [+]print(a) # [' Spa ', ' egg ', [+]
Some basic operations of the list
Index, Slice
A = ['spa'egg', [+]print(a[1]) #eggprint(a[:1])#[' Spa ']
modifying element values
A = ['spa'egg',]a[2] =print(a #[' Spa ', ' egg ', [+]
adding elements
1.append () new element added to the tail of the list
A = ['spa'egg',]a.append ('new print(a)#[' Spa ', ' egg ', ' + ', ' new ']
2.insert () insert to specified position
A = ['spa'egg',]a.insert (1) Print(a)#[' Spa ', ' the ' egg ' , ' the '
3.extend () extension, appending multiple values from another sequence at the end of the list
A = ['spa'egg',['ABB ',]a.extend (b)print(a)#[' Spa ', ' egg ', ', ', ' ABB ', 99]
Delete
1.del deletes the element at the specified index
A = ['spa'egg', [+]del a[1] Print (a) # [' Spa ', [+]
2. Pop () Delete one element at the tail of the list
A = ['spa'egg',]a.pop ()print(a #[' Spa ', ' egg ', '
3. Remove () deletes the specified element
A = ['spa'egg',]a.remove Print(a)#[' Spa ', ' egg ', [+]
Find-index (value, Start=none, Stop=none) find a value from the list the index position of the first occurrence ps:start<= find range <stop
A = ['Spa','Egg', 12, 34]Print(A.index (12))#2A = ['Spa','Egg', 12, 34,'Egg']Print(A.index ('Egg', 2, 5))#4
Reverse-reverse ()
A = ['spa'egg'] A.reverse ()print(a)#[' egg ', ', ' egg ', ' spa ']
Sort-sort ()
A = ['spa'egg ' the ' ' Egg ' ]a.sort ()print(a)#[' A ', ' "', ' egg ',' egg ', ' spa ']
Length-len ()
A = ['spa'egg ' the ' ' Egg ' ]print(Len (a))#5
Meta-group
Tuples are similar to lists, except that elements of tuples cannot be modified.
Tup1 = ('spa'egg' "egg")# when only one element is included in a tuple, You need to add a comma after the element tup2 = (50,)
View tuples
Tup1 = ('Spa','Egg',' A',' the','Egg')Print("Tup1[1]:", tup1[1])Print("Tup1[1:3]:", Tup1[1:3])#tup1[1]: Egg#Tup1[1:3]: (' egg ', ' a ')
Tuple connections
Tup1 = ('spa'egg' "egg"= (tup1+) tup2 Print (TUP3) # (' spa ', ' egg ', ' a ', ' ' ", ' egg ', ', ')
Delete-tuple elements cannot be deleted, but tuples can be deleted
Tup1 = ('spa'egg' "egg")print(tup1) del tup1print(tup1)
Execute exception, tup1 not found
Print (tup1) nameerror:name ' tup1 ' is not defined
Statistic-count (value), which counts the number of specified elements
Tup1 = ('spa'egg' "egg")print(Tup1.count ( ' Egg ' )#2
Dictionary
A dictionary is another mutable container model, and can store any type of object, such as another container model. A dictionary consists of a pair of keys and corresponding values.
Each key and value is separated by a colon (:), each pair is separated by commas, and the whole is placed in curly braces ({}).
The key must be unique, but the value does not have to be.
The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.
Dict1 = {'a'b'c': 3}
Access Dictionary-[key], if key does not exist in the dictionary, an exception will be thrown.
Dict = {'Name':'King',' Age': 21,'Class':'2-9'}Print("Name:", dict['Name'])Print("Age :", dict[' Age'])Print("Class:", dict['Class'])#Output ResultsName:kingage:21stClass:2-9
Modify Dictionary
Dict1 = {' Name ': ' King ', ' age ': +, ' Class ': ' 2-9 '}dict1[' age '] = 18# modify agedict1[' School '] = "DPS School" # ADD new ENTRYPR Int ("Age:", dict1[' age ') print ("School:", dict1[' School ']) #输出结果Age: 18School: DPS School
Get () returns the Val corresponding to key, if key does not exist, returns none
Dict1 = {' Name ': ' King ', ' age ': +, ' Class ': ' 2-9 '}print ("Age:", Dict1.get ("Age")) #输出结果Age: 21
Fromkeys (SEQ, value)
Seq = (' name ', ' age ', ' sex ') dict = Dict.fromkeys (seq, "value") print ("New Dictionary:%s"% str (dict)) #输出结果New Dictio nary: {' sex ': ' value ', ' name ': ' Value ', ' age ': ' Value '}
VALUES () returns all values in the dictionary
Dict1 = {' Name ': ' King ', ' age ': +, ' Class ': ' 2-9 '}print (dict1.values ()) #输出结果dict_values ([' 2-9 ', +, ' King '])
python-lists, tuples, dictionaries