2019.08.05 learning finishing

2019.08.05 learning finishing

Built-in method list type (list)

1. Usage: more equipment, more loving, and more courses, and even more girlfriend

2. Definitions: a plurality of any type can have the value of [], the elements separated by commas

3. common operations + built-in method: common operations and built-in method is divided into priority master (today have to remember), need to have (within a week to remember), other operations (understand) in three parts.

3.1 Priority grasp

  1. By index value (positive value + antiporter value), may be taken to deposit
  2. slice
  3. Length len
  4. Members and not in operation in
  5. Add append
  6. Delete del
  7. cycle

1. Press the index value (positive value + antiporter value), may be taken to deposit

# list之索引取值
name_list = ['nick', 'jason', 'tank', 'sean']
name_list[0] = 'nick handsom'
# name_list[1000] = 'tank sb'  # 报错

print(f"name_list[0]: {name_list[0]}")

name_list[0]: nick handsom

2. Slice

# list之切片
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list[0:3:2]: {name_list[0:3:2]}")

name_list[0:3:2]: ['nick', 'tank']

3. length

# list之长度
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"len(name_list): {len(name_list)}")

only (NAME_LIST): 4

4. Members and not in operation in

# list之成员运算in和not in
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"'tank sb' in name_list: {'tank sb' in name_list}")
print(f"'nick handsome' not in name_list: {'nick handsome' not in name_list}")

'tank sb' in name_list: False
'nick handsome' not in name_list: True

The added value

# list之追加值
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.append('tank sb')

print(f"name_list: {name_list}")

name_list: ['nick', 'jason', 'tank', 'sean', 'tank sb']

6. Delete

# list之删除
name_list = ['nick', 'jason', 'tank', 'sean']
del name_list[2]

print(f"name_list: {name_list}")

name_list: ['nick', 'jason', 'sean']

7. cycle

# list之循环
name_list = ['nick', 'jason', 'tank', 'sean']

for name in name_list:
    print(name)

nick
jason
tank
sean

3.2 need to know

  1. insert
  2. pop
  3. remove
  4. count
  5. index
  6. clear
  7. copy
  8. extend
  9. reverse
  10. sort

1.insert()

# list之insert()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.insert(1, 'handsome')

print(f"name_list: {name_list}")

name_list: ['nick', 'handsome', 'jason', 'tank', 'sean']

2.pop()

# list之pop(),pop()默认删除最后一个元素
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.pop(1): {name_list.pop(1)}")
print(f"name_list: {name_list}")

name_list.pop(1): jason
name_list: ['nick', 'tank', 'sean']

3.remove()

# list之remove()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.remove('nick'): {name_list.remove('nick')}")
print(f"name_list: {name_list}")

name_list.remove('nick'): None
name_list: ['jason', 'tank', 'sean']

4.count()

# list之count()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.count('nick'): {name_list.count('nick')}")

name_list.count('nick'): 1

5.index()

# list之index()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.index('nick'): {name_list.index('nick')}")

name_list.index('nick'): 0

6.clear()

# list之clear()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.clear()

print(f"name_list: {name_list}")

name_list: []

7.copy()

# list之copy()
name_list = ['nick', 'jason', 'tank', 'sean']

print(f"name_list.copy(): {name_list.copy()}")
name_list.copy(): ['nick', 'jason', 'tank', 'sean']

8.extend()

# list之extend()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list2 = ['nick handsome']
name_list.extend(name_list2)

print(f"name_list: {name_list}")

name_list: ['nick', 'jason', 'tank', 'sean', 'nick handsome']

9.reverse()

# list之reverse()
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.reverse()

print(f"name_list: {name_list}")

name_list: ['sean', 'tank', 'jason', 'nick']

10.sort()

# list之sort(),使用sort列表的元素必须是同类型的
name_list = ['nick', 'jason', 'tank', 'sean']
name_list.sort()

print(f"name_list: {name_list}")

name_list.sort(reverse=True)
print(f"name_list_reverse: {name_list}")

name_list: ['jason', 'nick', 'sean', 'tank']
name_list_reverse: ['tank', 'sean', 'nick', 'jason']

4. The stored value or a plurality of values: a plurality of values

5. orderly or disorderly: Ordered

6. Variable or immutable: Variable Data Type

Tuple type built-in method (tuple)

1. Usage: more equipment, more loving, and more courses, and even more girlfriend

2. Definition: () can have the values ​​of a plurality of any type, a comma-separated elements

3. The method of common operations built +: built-in method and common operations:

3.1 Priority grasp

  1. Index values
  2. Sections (care regardless of the end, step)
  3. Length len
  4. Members and not in operation in
  5. cycle
  6. count
  7. index

1. index value

# tuple之索引取值
name_tuple = ('nick', 'jason', 'tank', 'sean')
# name_tuple[0] = 'nick handsom'  # 报错

print(f"name_tuple[0]: {name_tuple[0]}")

name_tuple[0]: nick

2. Section (care regardless of the end, step)

# tuple之切片
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple[1:3:2]: {name_tuple[1:3:2]}")

name_tuple[1:3:2]: ('jason',)

3. length

# tuple之长度
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"len(name_tuple): {len(name_tuple)}")

only (name_tuple): 4

4. The members of the operation

# tuple之成员运算
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"'nick' in name_tuple: {'nick' in name_tuple}")

'nick' in name_tuple: True

5. cycle

# tuple之循环
name_tuple = ('nick', 'jason', 'tank', 'sean')

for name in name_tuple:
    print(name)

nick
jason
tank
sean

6.count()

# tuple之count()
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple.count('nick'): {name_tuple.count('nick')}")

name_tuple.count('nick'): 1

7.index()

# tuple之index()
name_tuple = ('nick', 'jason', 'tank', 'sean')

print(f"name_tuple.index('nick'): {name_tuple.index('nick')}")

name_tuple.index('nick'): 0

4. The stored value or a plurality of values: a value

5. orderly or disorderly: Ordered

6. The variable or non-variable: immutable data type

The difference between tuples and lists of

Variable is a list of reasons: the memory address corresponding to the index value may be varied

Tuples can not become the reason is: the index of the corresponding memory address values ​​can not change, or conversely, as long as the memory address corresponding to the index value has not changed, then the tuple is never changed.

Dictionary type built-in method (dict)

1. Purpose: a plurality of stored values, but each has a value of a corresponding key, key values ​​described functions. It is used for different states when the stored value indicates, for example, the value stored there name, age, height, weight, hobbies.

2. Definitions: within {} are separated by commas plurality of elements, each element is a key: in the form of value, the data value may be any type, but generally should be a string type key, but must change the type of key is not available .

3. common operations + built-in method: built-in methods and common operations are divided into priority master (today have to remember), need to know (remember the week) in two parts.

3.1 Priority grasp

  1. Access key press value: deposit may be desirable
  2. Length len
  3. Members and not in operation in
  4. Delete del
  5. Button keys (), the value of values ​​(), on the key-value items ()
  6. cycle

1. Press the key access Found: deposit may be desirable

# dic之按key存取值
dic = {'a': 1, 'b': 2}

print(f"first dic['a']: {dic['a']}")

dic['a'] = 3

print(f"second dic['a']: {dic['a']}")

first dic['a']: 1
second dic['a']: 3

2. length len

# dic之长度len
dic = {'a': 1, 'b': 2}

print(f"len(dic): {len(dic)}")

only (DIC): 2

3. The members of the operations in and not in

# dic之成员运算in和not in
dic = {'a': 1, 'b': 2}

print(f"'a' in dic: {'a' in dic}")
print(f"1 in dic: {1 in dic}")

'a' in dic: True
1 in dic: False

4. Delete

# dic之删除del
dic = {'a': 1, 'b': 2}
del dic['a']

print(f"dic.get('a'): {dic.get('a')}")

dic.get('a'): None

# dic之删除pop()
dic = {'a': 1, 'b': 2}
dic.pop('a')  # 指定元素删除

print(f"dic.pop('b'): {dic.pop('b')}")
print(f"dic.get('a'): {dic.get('a')}")

dic.pop('b'): 2
dic.get('a'): None

# dic之删除popitem()
dic = {'a': 1, 'b': 2}

print(f"dic.popitem(): {dic.popitem()}")  # popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)。

dic.popitem(): ('b', 2)

The key keys (), the value of values ​​(), on the key-value items ()

# dic之键keys()、值values()、键值对items(),python2中取出的是列表(鸡蛋);python3中取出的是元组(鸡)
dic = {'a': 1, 'b': 2}

print(f"dic.keys(): {dic.keys()}")
print(f"dic.values(): {dic.values()}")
print(f"dic.items(): {dic.items()}")

dic.keys(): dict_keys(['a', 'b'])
dic.values(): dict_values([1, 2])
dic.items(): dict_items([('a', 1), ('b', 2)])

6. cycle

# dic之循环
# dic是无序的,但是python3采用了底层优化算法,所以看起来是有序的,但是python2中的字典是无序
dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

for k, v in dic.items():  # items可以换成keys()、values()
    print(k, v)

a 1
b 2
c 3
d 4

3.2 need to know

  1. get
  2. update
  3. fromkeys
  4. setdefault

1.get()

# dic之get()
dic = {'a': 1, 'b': 2}

print(f"dic.get('a'): {dic.get('a')}")
print(f"dic.get('c'): {dic.get('c')}")

dic.get('a'): 1
dic.get('c'): None

2.update()

# dic之update()
dic1 = {'a': 1, 'b': 2}
dic2 = {'c': 3}
dic1.update(dic2)

print(f"dic1: {dic1}")

dic1: {'a': 1, 'b': 2, 'c': 3}

3.fromkeys()

# dic之fromkeys()
dic = dict.fromkeys(['name', 'age', 'sex'], None)

print(f"dic: {dic}")

dic: {'name': None, 'age': None, 'sex': None}

4.setdefault()

# dic之setdefault(),有指定key不会改变值;无指定key则改变值
dic = {'a': 1, 'b': 2}

print(f"dic.setdefault('a'): {dic.setdefault('a',3)}")
print(f"dic: {dic}")
print(f"dic.setdefault('c'): {dic.setdefault('c',3)}")
print(f"dic: {dic}")

dic.setdefault('a'): 1
dic: {'a': 1, 'b': 2}
dic.setdefault('c'): 3
dic: {'a': 1, 'b': 2, 'c': 3}

4. The stored value or a plurality of values: a plurality of values, the value may be a plurality of types, key must be immutable type, generally should be immutable types string type

5. order or disorder: disordered

6. Variable or immutable: Variable Data Type

Set type built-in method

1. Use: relationship for assembly operations, since the elements in the set unordered collection of elements and can not be repeated, and therefore can be set to the weight, but will re-set to disrupt the order of the original elements.

2. Definitions: within {} are separated by commas plurality of elements, each element must be immutable.

3. common operations + built-in method: built-in methods and common operations are divided into priority master (today have to remember), you need to know (remember the week) in two parts

3.1 Priority grasp

  1. Length len
  2. Members and not in operation in
  3. | Union, union
  4. & Intersection, intersection
  5. -差集、difference
  6. ^ Symmetric difference, symmetric_difference
  7. ==
  8. Parent Set:>,> =, issuperset
  9. Subsets: <, <=, issubset

1. The length len

# set之长度len
s = {1, 2, 'a'}

print(f"len(s): {len(s)}")

len (s): 3

2. Members in operation and not in

# set之成员运算in和not in
s = {1, 2, 'a'}

print(f"1 in s: {1 in s}")

1 in s: True

3.2 need to know

  1. add
  2. remove
  3. difference_update
  4. discard
  5. isdisjoint

1.add()

# set之add()
s = {1, 2, 'a'}
s.add(3)

print(s)

{1, 2, 3, 'a'}

2.remove()

# set之remove()
s = {1, 2, 'a'}
s.remove(1)

print(s)

{2, 'a'}

3.difference_update()

# str之difference_update()
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.difference_update(linuxers)

print(f"pythoners.difference_update(linuxers): {pythoners}")

pythoners.difference_update(linuxers): {'tank', 'jason', 'sean'}

4.discard()

# set之discard()
s = {1, 2, 'a'}
# s.remove(3)  # 报错
s.discard(3)

print(s)

{1, 2, 'a'}

5.isdisjoint ()

# set之isdisjoint(),集合没有共同的部分返回True,否则返回False
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.isdisjoint(linuxers)

print(f"pythoners.isdisjoint(linuxers): {pythoners.isdisjoint(linuxers)}")

pythoners.isdisjoint(linuxers): False

4. The stored value or a plurality of values: a plurality of values, and is immutable.

5. order or disorder: disordered

6. Variable or immutable: Variable Data Type

Guess you like

Origin www.cnblogs.com/zhangmingyong/p/11303486.html