7.18 part of the container built-in method of learning

7.18 part of the container built-in method of learning

dictionary

  1. Role: value too easy to keep a list, use a dictionary to take trouble

  2. Defined method:

dic = {
'name':'nick',
'height':180,
'weight':140,
'hobby_list':['read','run','music','fishing','programming','coding','debugging']
}

for k,v in dic():
    print(k,v)

eg: basic use:

wzh = {'name':'wangzhihui','age':18,'sex':'nan','height':201,'hobby':['sing','dance','rap','basketball']}
print(wzh.get('hobby')[3])
print(wzh['name'])
print(wzh.items()) ## 获取键值对
print(wzh.keys())  ## 获取关键字
print(wzh.values())  ## 获取值

Dictionary part built-in method

  1. method

    1. Priority grasp
      1. Key Value
      2. length
      3. keys/values/items
      4. for loop
      5. Member operator
    2. Need to know
      1. pop: Removing elements
      2. fromkeys: generating a list of a dictionary, the default value None
      3. setdefault: There does not change the specified key value; no specified key value is changed
      4. get: Gets a value by key, no key default fetch None
      5. update: Extended Dictionary
      6. del: delete key-value pairs dictionary
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: disordered
  4. Variable or immutable: variable

List

Is defined by: a plurality of spaced apart elements of any data type comma in []

friends_list = ['longzeluola','canglaoshi','qiaobenai','nick']
lis = list('abcd')

A list of built-in method

  1. method:
    1. Priority grasp
      1. Index value (in turn can take change)
      2. slice
      3. length
      4. append
      5. Member operator
      6. for loop
    2. Need to know
      1. count: Count the number of elements
      2. remove: remove elements
      3. reverse: reverse list
      4. pop: Removing elements
      5. insert: insert elements
      6. sort: sort the list
      7. index: Index element
      8. del: remove elements
      9. extend: an extended list
      10. clear: Clears the list
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: Ordered
  4. Variable or immutable: variable

Tuple

  1. Role: similar to the list, you can not keep taking
  2. Defined method:
friends_tuple = ('longzeluola','canglaoshi','qiaobenai','nick')
tup = tuple('abcd')

Built-in methods tuple

  1. method
    1. Priority grasp
      1. Index values ​​(can not be changed)
      2. slice
      3. length
      4. Member operator
      5. for loop
      6. count: Count the number of elements
      7. index: the index element position
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: Ordered
  4. Variable or immutable: immutable variable that says no

set

  1. Action: a plurality of stored values ​​to be set between the operation
  2. Defined method:
s1 = {'nick','wuhao','rocky','owen','all is dsb','right'}
s1.add('nmsl')
s1.discard('right')
 print(s1)

A collection of built-in method

  1. method:
    1. Priority grasp
      1. Deduplication
      2. Union |
      3. Intersection &
      4. Difference set -
      5. Symmetric difference ^
      6. Superset> emsp &;> =
      7. 子集 <&emsp;<=
      8. ==
    2. Need to know
      1. add: add an element
      2. difference_update: update the elements of the collection not
      3. isdisjoint: If the two elements of the intersection is empty return True, otherwise False
      4. remove: Removes the element does not exist error value #
      5. discard: remove elements not being given #
  2. Value of a plurality of values ​​or: a plurality of values
  3. Ordered or disordered: disordered
  4. Variable or immutable: variable

Guess you like

Origin www.cnblogs.com/dadazunzhe/p/11209684.html