Python basic knowledge of the entire network most 3 (internal method) Full

1. Integrated

  • 1. type (**** data) : the type of data acquired

  • 2. the isinstance () : Check the element type, returns a Boolean value
    Example:

a = '小甲鱼'   
isinstance(a,str)         # 结果为 True
  • 3. the Assert : the Assert (assertion) conditions. When the conditional statement is false, the program terminates with an exception

  • 4. len (demo) : to give demo array length (or strings)

  • 5.range([start],stop,[step=1]):

Generating a start parameter value from the start to the end value of the stop parameter sequence of numbers, start, step and step are optional default value of 1

  • 6. the append () : add an element to the list

  • 7. The Extend () : Extend the parameter (a new list) is added to the list (i.e. two connection list)

  • 8. The INSERT () : INSERT (position of elements) specified position about to insert elements into

  • 9. the Remove () : the Remove (element) to remove this element from the list

  • 10. The del : delarr [i]: the subscript deletion list element i

del arr: 清空数组,删除所有元素
  • 11.pop():

arr.pop () : Delete the last element in the array arr and returns the value of the element
arr.pop (i) : Remove array subscript of arr element i and returns the value of the element

  • 12.not in:
123 not in arr:如果数组中没有123这个元素,返回False。与in相反
  • 13. COUNT () : number of calculated line element in the array, for example: arr.count (element);

  • 14. A index () : arr.index (123): Returns the index 123 in the first occurrence of the array arr position
    arr.index (123,3,7): 123 in arr [. 3] and aaa [6] subscript position between the first occurrence

  • 15. A Reverse () : Array reversed.

  • 16.sort():

arr.sort(): 对arr进行排序(默认从小到大)
arr.sort(reverse=True):  对arr进行排序(从大到小)
  • 17. A filter ()
    Example:
list(filter(None,[1,0,False,True])) à [1,True]
list( filter ( lambda x : x % 2 ), range(10) )à [1,3,5,7,9]
  • 18. Map ()
    Example:
list( map( lambda x : x * 2,range(10)))
等价于:
     [ 0 , 2 , 4, 6 , 8 , 10 , 12 , 14 , 16 , 18 ]
  • 19. A STR () : returns a string format object

2, string

1. casefold () : str.casefold (): the string str in all capital letters to lower case
2. the format () :

'{0} love {1}.{2}'.format('I','FishC','com')  
'{a} love {b}.{c}'.format(a='I',b='FishC',c='com')
'{0} love {b}.{c}'.format('I', b='FishC',c='com')
'{a} love {b}.{0}'.format(a='I',b='FishC','com')
以上四条结果均为: I love FishC.com

'{{0}}'.format('不打印')              结果为:{0}'{0:.lf}{1}'.format(27.658,'GB')      结果为:27.7GB

Symbol Meaning string formatting
Example:

'%c %c %c'% (97,98,99)   -> 'a b c'
'%d + %d = %d' %(4,5,4+5) -> '4 +5 = 9'

3, the sequence

  • 1. List () :
    Example:
a = 'a b c'   
list(b): ['a',' ','b',' ','c']
c = (1,2,3)   
list(c): [ 1 , 2 , 3 ]
  • 2. max (arr) : Returns the maximum value in the array arr

  • 3.sum(arr,[index]):

sum (arr): Returns the total number of arr
sum (arr, 8): Returns the total number of arr +8

  • 4.enumerate(arr):
a = [ 6 , 2 , 4 ]           #返回的是一个对象
list(enumerate(a)): [(0,6),(1,2),(2,4)]
  • 5. The the sorted (arr) : Sort the list arr

  • 6. The the reversed (arr) : arr inverted list, it returns an object

  • 7. Zip () :
    Example:

a = [2,3,4,5,6]  b = [7,8,9]
list( zip(a , b) ): [ (2,7) , (3,8) , (4,9) ]

4, map embedded dictionary methods (key-value set)

  • 1. fromkeys ()
    statement:
demo = {}
demo.fromkeys( (1,2,3) , ”哈哈” )
结果: {1:'哈哈',2:'哈哈',3:'哈哈'}
  • 2.keys(): demo.keys()= [ 1 , 2 , 3 ]

  • 3. values () : demo.values () = 'ha', 'ha', 'ha']

  • 4. items () : demo.items () = [(. 1, 'ha'), (2, 'ha'), (3, 'ha')]

  • 5.get():

用法一:
     demo.get(key)               #查找key对应的value
用法二:
     demo.get(key,defaultvalue)  #查找key对应的value,如果没有此key,就用defaultvalue充当默认值
  • 6. the Clear () :
    Empty dictionary:
b={1:'la',2:'sese'}
a= b
a.clear()  结果: a = {}  b = {}
  • 7. Copy () : Copy the dictionary

  • 8.pop():

用法一: 
     a.pop(key)      #弹出key对应的item
用法二: 
     a.pop()         #随机弹出
  • 9.setdefault():\
a.setdefault('abc')         #字典里会多出一个{‘abc’,None}
a.setdefault(5,'def')       #字典里会多出一个{ 5 , ’def’ }
  • 10.update():
a = {1:'one','小白':'two'}
b = { ‘小白’ : ‘小狗’ }
a.update(b)    等价于: a = {1:'one','小白':'小狗'}

5, set collection

  • 1. Create:
#一种是直接把一队元素用花括号括起来
num1 = { 1 , 2 , 3 , 4 , 5 }
#另一种是使用set()工厂函数
num2 = set( [ 1 , 2 , 3 , 4 , 5 ])
  • 2.add (elements): adding an element to the set collection

  • 3.remove (elements): removing the element from the collection set

  • 4.frozenset (): method defined in the same set (), the definition of a set of elements of the set of immutable

Guess you like

Origin blog.csdn.net/affluent6/article/details/91534112