day08 learning finishing -Python built-in method

2019/08/05 Learning finishing

Python built-in method

List of data types built-in method

list

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

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

# my_hobby = list(['sing','dancing','rap'])
my_hobby = ['sing', 'dancing', 'rap']

print(f"my_hobby: {my_hobby}")
my_girl_friend: ['sing', 'dancing', 'rap']
l = list('hello 胡歌')
print(f"l: {l}")
l: ['h', 'e', 'l', 'l', 'o', ' ', '胡', '歌']

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

Priority control (*****)

  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)}")
len(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

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

hobby_list = ['read', 'run', 'girl']
print(f'first:{id(hobby_list)}')
hobby_list[2] = ''
print(f'second:{id(hobby_list)}')
first:4522187016
second:4522187016

6. Variable or immutable: Variable Data Type

Tuple data type built-in method

tuple

Tuples are immutable list, i.e., value tuples can not be changed, and therefore generally used only for only the tuple memory requirements are not taken. Thus tuples may also be substituted out list, so compared to the list of tuples used rarely.

For example, define multiple acts like a list of hobbies

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

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:

  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

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

5. orderly or disorderly: Ordered

name_tuple = ('nick',)
print(f'first:{id(name_tuple)}')
first:4394454152

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

Dictionary data 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 .

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

print(f"dic: {dic}")
dic: {'a': 1, 'b': 2}

3. Method built + common operations: Common

  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)}")
   len(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()}")  # 随机删除一个元素,无法指定
   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

other

  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}

A set of built-in data types Method

set

Set (SET)
1. different elements
2. unordered
3. collection element must be immutable
4. The de-emphasis characteristic having

Defined method: s = {1,3,4,5}, s = set ( 'hello')
defines a set of immutable: s = frozenset ( 'hello' )

class set(object):
    """
    set() -> new empty set object
    set(iterable) -> new set object
    
    Build an unordered collection of unique elements.
    """
    def add(self, *args, **kwargs): # real signature unknown
        添加
        pass

    def clear(self, *args, **kwargs): # real signature unknown
        清空
        pass

    def difference(self, *args, **kwargs): # real signature unknown
        差集  -
        pass

    def difference_update(self, *args, **kwargs): # real signature unknown
        差集并更新,结果赋值到当前值
        pass

    def discard(self, *args, **kwargs): # real signature unknown
        删除
        删除元素不存在不会报错
        pass

    def intersection(self, *args, **kwargs): # real signature unknown
        交集  &
        pass

    def intersection_update(self, *args, **kwargs): # real signature unknown
        交集并更新,结果赋值到当前值
        pass

    def isdisjoint(self, *args, **kwargs): # real signature unknown
        如果没有交集返回true,有返回false
        pass

    def issubset(self, *args, **kwargs): # real signature unknown
        相当于s1<=s2,s1是s2的子集,s2是s1的父集
        pass

    def issuperset(self, *args, **kwargs): # real signature unknown
        相当于s1>=s2,s1是s2的父集,s2是s1的子集
        pass

    def pop(self, *args, **kwargs): # real signature unknown
        删除
        pass

    def remove(self, *args, **kwargs): # real signature unknown
        删除指定元素
        删除元素不存在会报错
        pass

    def symmetric_difference(self, *args, **kwargs): # real signature unknown
        交叉补集    "并集—交集" ^
        pass

    def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
        交叉补集并更新
        pass

    def union(self, *args, **kwargs): # real signature unknown
        并集  |
        pass

    def update(self, *args, **kwargs): # real signature unknown
        更新
        pass

Guess you like

Origin www.cnblogs.com/Wunsch/p/11303009.html