python data types and built-in method
First, the list (continued)
list.count( )、list.index( )
= List [ '. 1', '2', '. 3', '2', 'A', 'B', 'C', 'A'] Print (list.count ( 'A')) for statistical # the number of an element of the print (list.index ( 'a', 4,)) # check the specified character position of the first index, if it will not find the error, you can specify the look
list.sort( ) 、sorted(list)
Difference: sorted (List) then drained will generate a new list, list.sort () is in the original list of operations.
= List2 [. 1,. 4,. 3,. 5,. 7] list2.sort (reverse = True) # If the reverse parameter is not written, the default is False, the ASCII code from small to large print (list2) # [7, 5, 4 ,. 3,. 1] list3 = [. 1,. 4,. 3,. 5,. 7] Print (the sorted (list3, Reverse = True)) # [. 7,. 5,. 4,. 3,. 1] Print (list3) # [. 1,. 4 , 3, 5, 7]
list.clear () Clear
list4 = [1, 4, 3, 5, 7] list4.clear() print(list4) # []
list.reverse () reverse the sort
list4 = [1, 4, 3, 5, 7] list4.reverse() print(list4) # [7, 5, 3, 4, 1]
Second, the tuple tuple
Definition: storing data by parentheses, separated by commas between the data value tuple is not allowed to change.
Tuples can take the values by index
tuple = (1, 2, 3) print(tuple[0]) # 1
Slice index (not take take the first tail)
tuple = (1, 2, 3) print(tuple[0:1]) # (1,)
Note: If only one element in a tuple, must be marked with a comma at the end, otherwise it will default to a string
tuple2 = (1) tuple3 = (1,) print(type(tuple2)) # <class 'int'> print(type(tuple3)) # <class 'tuple'>
Member operator in; not in
tuple = (1, 2, 3) print( 1 not in tuple) # False
Number len () element
tuple = (1, 2, 3) print(len(tuple)) # 3
The method of carrying tuple tuple.count (), tuple.index ()
tuple = ('1', '2', '3', '2', 'a', 'b', 'c', 'a') print(tuple.count('a')) # 2 print(tuple.index('a')) # 4 print(tuple.index('a', 5,)) # 7
Summary: tuples ordered, immutable, stored plurality of values
Third, Dictionary
Dictionary definition in three ways:
'' ' Dictionary definition of three ways ' '' dict1 = { 'name': 'ABC', 'Age': 16} Print (dict1) # { 'name': 'ABC', 'Age': 16} dict2 = dict ({ 'name': 'ABC', 'Age': 16}) Print (dict2) # { 'name': 'ABC', 'Age': 16} List1 = [ 'name', 'Age'] List2 = [ 'ABC', 16] dict4 = ZIP (List1, List2) for J in dict4: Print (J, type (J), End = '',) # ( 'name', 'ABC') <class' tuple '> (' Age ', 16) <class' tuple '> Print () dict5 = dict (zip (List1, List2)) # by dict) converting (zip type to type dict print (dict5) # {' name ':' abc ',' age ':16}
dict.get( )
''' get 方法 ''' dict1 = {'name': 'abc', 'age': 16} print(dict1.get('name')) # abc print(dict1.get('name','789')) # abc print(dict1.get('gender', 'male')) # male
dict.get () usage scenarios:
The first argument to get the user interface to make the incoming variable, as a key dict, the second argument made errors. If the variable is not passed in the dict key, an error message is output.
dict.setdefault( )
Note setdefault () and get () difference
'''
get 方法
setdefault方法
辨析
'''
dict1 = { }
print(dict1.get('name')) # None ()默认值
print(dict1) # { }
print(dict1.get('name', 'abc')) # abc
print(dict1) # { }
dict2 = { }
print(dict2.setdefault('name')) # None ()默认值
print(dict2) # {'name': None}
print(dict2.setdefault('name', 'abc')) # None
print(dict2) # {'name': None}
print(dict2.setdefault('gender', 'male')) # male
print(dict2) # {'name': None, 'gender': 'male'}
to sum up:
Use dict.get () method, dict will not have any impact.
If you get the first key parameter in the dict, dict the value is returned, otherwise the second parameter of the get method, without parameters, a default value is returned None ;
Use dict.getdefault () method, dict will be affected.
If the first parameter setdefault key in the dict, dict the return value; otherwise, get the second parameter of the method, if there is no parameter, None is returned , at the same time, the key and the second parameter (the default None ) into dict key-value pairs to form;
dict.keys( ) 、dict.values( ) 、dict.items( )
''' dict.keys() 取所有的key dict.values() 取所有的value dict.items() 取所有的键值对 ''' dict1 = {'name': 'abc', 'age': 16} print(dict1.keys()) # dict_keys(['name', 'age']) for key in dict1.keys(): print(key, end=' ') # name age print(dict1.values()) # dict_values(['abc', 16]) for value in dict1.values(): print(value, end=' ') # abc 16 print(dict1.items()) # dict_items([('name', 'abc'), ('age', 16)]) for key, value in dict1.items(): print(key, value, end=' ') # name abc age 16
dict.pop( key )
Delete the specified key, dict.pop (key) returns a value corresponding to the key value
dict.popitem( )
Randomly delete a key-value pair dict.popitem () Returns the value of the key-value pair is deleted, the type of the return value of a tuple into a tuple
dict1.update( dict2 )
'' '
Dict1.update (dict2)
with dict2 updated dict1 Briefly, the sequence is inserted dict2 dict1, the same key value is updated, without increasing the key-value.
'' '
Dict1 = {' name ':' ABC ',' Age ': 16}
dict2 = {' name ':' Zhang '}
dict3 = {' Gender ':' Femal '}
dict3.update (dict1)
Print ( dict3) {# 'Gender': 'Femal', 'name': 'ABC', 'Age': 16}
dict1.update (dict2)
Print (dict1) # { 'name': 'Zhang', 'Age': 16}
dict.fromkeys ( parameter1 , parameter2 ) parameter1 must be iterables
Fromkeys generated by dictionary, Key is parameter1 the respective elements , all of value all parameter2
dict1 = dict.fromkeys(range(3), 'csic') print(dict1) # {0: 'csic', 1: 'csic', 2: 'csic'}
Summarize dictionary unordered, variable, store multiple values
Fourth, the collection
Use is set to a weight and relational operators
Collection, each element of a comma-delimited by braces storage,
When you define an empty set, you must use the set () mode
ss1 = set (range (3) ) # set () parameters must be iterative object, or null Print (SS1) # {0,. 1, 2} Print (type (SS1)) # <class 'SET'>
Set of elements which can not store duplicate
ss2 = {1, 2, 3, 1, 2, 1, 3} print(len(ss2)) # 3 print(ss2) # {1, 2, 3}
to sum up
set of collection is not required, a plurality of stored values, variable, the variable is limited to a set of add and remove, it can not be modified prior to the collection of elements.
frozenset collection is frozen, it is immutable, there is a hash value, the benefit is that it can be used as a dictionary key, can be used as other elements of the collection. Once created can not be changed, not add, remove method.