eight

Advanced python

First, the list of types of built-in method

1.1 Use

Used to describe more equipment, more loving, more courses.

1.2 Definitions

There may be any type of a plurality of values ​​[], the comma-segmentation.

name_list = ['panghu','xiaofu','jingxiang','daxiong']
print(F"{name_list[1]}")

xiaofu

1.3 + common operations built-in method

1.3.1 Priority grasp

  1. By index values ​​(n + reverse-phase), it is also desirable to deposit

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(F"{name_list[-1]}")
    
    daxiong
  2. slice

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(F"{name_list[0:3:2]}")
    
    ['panghu', 'jingxiang']
  3. length

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(F"{len(name_list)}")
    
    4
  4. Members and not in operation in

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(F"{'daxiong' in name_list}")
    print(F"{'pangmei' in name_list}")
    
    True
    False
  5. Added value

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    name_list.append('xiaodingdang')
    print(F"{name_list}")
    
    ['panghu', 'xiaofu', 'jingxiang', 'daxiong', 'xiaodingdang']
  6. delete

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    del name_list[3]
    print(F"{name_list}")
    
    ['panghu', 'xiaofu', 'jingxiang']
  7. cycle

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    for name in name_list:
        print(name)
    
    panghu
    xiaofu
    jingxiang
    daxiong

1.3.2 need to know

  1. insert()

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    name_list.insert(3,'xiaodingdang')
    print(f'{name_list}')
    
    ['panghu', 'xiaofu', 'jingxiang', 'xiaodingdang', 'daxiong']
  2. pop () (default removes the last item)

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(f'{name_list.pop(2)}')
    print(f'{name_list}')
    
    jingxiang
    ['panghu', 'xiaofu', 'daxiong']
  3. remove()

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{name_list.remove('xiaofu')}")
    print(f'{name_list}')
    
    None
    ['panghu', 'jingxiang', 'daxiong']
  4. count()

    name_list = ['panghu','xiaofu','xiaofu','jingxiang','daxiong']
    print(f"{name_list.count('xiaofu')}")
    
    2
  5. index () (lead cable)

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{name_list.index('xiaofu')}")
    
    1
  6. clear()

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    name_list.clear()
    print(f"{name_list}")
    
    []
  7. copy()

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{name_list.copy()}")
    
    ['panghu', 'xiaofu', 'jingxiang', 'daxiong']
  8. extend

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    name_list2 = ['xiaodingdang']
    name_list.extend(name_list2)
    print(f"{name_list}")
    
    ['panghu', 'xiaofu', 'jingxiang', 'daxiong', 'xiaodingdang']
  9. reverse () (flashback)

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    name_list.reverse()
    print(f"{name_list}")
    
    ['daxiong', 'jingxiang', 'xiaofu', 'panghu']
  10. sort

    name_list = ['panghu','xiaofu','jingxiang','daxiong']
    name_list.sort()
    print(f"{name_list}")
    
    ['daxiong', 'jingxiang', 'panghu', 'xiaofu']

Stored-value 1.4

A plurality of stored values

1.5 orderliness

Ordered

1.6 Variability

Variable

Second, the tuple type of built-in method

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. Tuple is a list of advantages compared to: modify the value list, the list structure will change, but only need to store tuples, in some extent, so the list require more memory. But now the industry is not a memory problem, so the industry group generally does not use Spring.

2.1 Use

More equipment, more loving, and more courses.

2.2 definitions

In () may have a plurality of values ​​of any type, a comma-separated elements.

2.3 + common operations built-in method

  1. Index values

    name_tuple = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{name_tuple}")
    
    ['panghu', 'xiaofu', 'jingxiang', 'daxiong']
  2. Sections (care regardless of the end, step)

    name_tuple = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{name_tuple[1:3:2]}")
    
    xiaofu
  3. length

    name_tuple = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{len(name_tuple)}")
    
    4
  4. Member operator

    name_tuple = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{'daxiong' in name_tuple}")
    
    True
  5. cycle

    name_tuple = ['panghu','xiaofu','jingxiang','daxiong']
    for name in name_tuple:
        print(name)
    
    panghu
    xiaofu
    jingxiang
    daxiong
  6. count()

    name_tuple = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{name_tuple.count('daxiong')}")
    
    1
  7. index()

    name_tuple = ['panghu','xiaofu','jingxiang','daxiong']
    print(f"{name_tuple.index('daxiong')}")
    
    3

Stored-value 2.4

Multiple values

2.5 orderliness

Ordered

2.6 Variability

Immutable data type

Third, the dictionary type of built-in method

3.1 Use

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.

3.2 definitions

A plurality of elements separated by commas within {}, each element is key: value form, value can be any data type, but generally should be a string type key, but the key must be immutable.

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

dic: {'a': 1, 'b': 2}


dic = dict(a=1, b=2, c=3)
print(f"dic: {dic}")

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


dic = {1: 'a', 0: 'b'}
print(f"dic[0]: {dic[0]}") 

dic[0]: b

3.3 + common operations built-in method

3.3.1 Key

  1. Access key press value: deposit may be desirable
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
  1. Length len
dic = {'a': 1, 'b': 2}
print(f"len(dic): {len(dic)}")

len(dic): 2
  1. Members and not in operation 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
  1. delete
dic = {'a': 1, 'b': 2}
del dic['a']
print(f"dic.get('a'): {dic.get('a')}")

dic.get('a'): None


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 = {'a': 1, 'b': 2}
print(f"dic.popitem(): {dic.popitem()}")  # popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)。

dic.popitem(): ('b', 2)
  1. Button keys (), the value of values ​​(), on the key-value items ()
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)])
  1. cycle
dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
for k, v in dic.items():  
    print(k, v)
    
a 1
b 2
c 3
d 4

3.3.2 need to know

  1. 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
  1. update()
dic1 = {'a': 1, 'b': 2}
dic2 = {'c': 3}
dic1.update(dic2)
print(f"dic1: {dic1}")

dic1: {'a': 1, 'b': 2, 'c': 3}
  1. fromkeys()
dic = dict.fromkeys(['name', 'age', 'sex'], None)
print(f"dic: {dic}")

dic: {'name': None, 'age': None, 'sex': None}
  1. setdefault()
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}

Fourth, the set of built-in method of the type

4.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 the weight set to disrupt the order of the original elements.

4.2 definitions

{} The plurality of elements spaced apart by a comma, each element must be immutable.

s = {1, 2, 1, 'a'}  
print(f"s: {s}")

s: {1, 2, 'a'}


s = {1, 2, 1, 'a', 'c'}
for i in s:
    print(i)
1
2
c
a


s = set('hello')
print(f"s: {s}")

s: {'e', 'o', 'h', 'l'}

4.3 built common operations and methods

4.3.1 Key

1. The length len

s = {1, 2, 'a'}
print(f"len(s): {len(s)}")

len(s): 3

2. Members in operation and not in

s = {1, 2, 'a'}
print(f"1 in s: {1 in s}")

1 in s: True

3. | union

pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners|linuxers: {pythoners|linuxers}")
print(f"pythoners.union(linuxers): {pythoners.union(linuxers)}")

pythoners|linuxers: {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
pythoners.union(linuxers): {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}

4. & intersection

pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners&linuxers: {pythoners&linuxers}")
print(f"pythoners.intersection(linuxers): {pythoners.intersection(linuxers)}")

pythoners&linuxers: {'nick'}
pythoners.intersection(linuxers): {'nick'}

5.- difference set

pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners-linuxers: {pythoners-linuxers}")
print(f"pythoners.difference(linuxers): {pythoners.difference(linuxers)}")

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

6. The symmetric difference ^

pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
print(f"pythoners^linuxers: {pythoners^linuxers}")
print(f"pythoners.symmetric_difference(linuxers): {pythoners.symmetric_difference(linuxers)}")

pythoners^linuxers: {'egon', 'tank', 'kevin', 'jason', 'sean'}
pythoners.symmetric_difference(linuxers): {'egon', 'tank', 'kevin', 'jason', 'sean'}

7.==

pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javers = {'nick', 'egon', 'kevin'}
print(f"pythoners==linuxers: {pythoners==linuxers}")
print(f"javers==linuxers: {javers==linuxers}")

pythoners==linuxers: False
javers==linuxers: True

8. superset:>,> =

pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javaers = {'jason', 'nick'}
print(f"pythoners>linuxers: {pythoners>linuxers}")
print(f"pythoners>=linuxers: {pythoners>=linuxers}")
print(f"pythoners>=javaers: {pythoners>=javaers}")
print(f"pythoners.issuperset(javaers): {pythoners.issuperset(javaers)}")

pythoners>linuxers: False
pythoners>=linuxers: False
pythoners>=javaers: True
pythoners.issuperset(javaers): True

9. subsets: <, <=

pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
javaers = {'jason', 'nick'}
print(f"pythoners<linuxers: {pythoners<linuxers}")
print(f"pythoners<=linuxers: {pythoners<=linuxers}")
print(f"javaers.issubset(javaers): {javaers.issubset(javaers)}")

pythoners<linuxers: False
pythoners<=linuxers: False
javaers.issubset(javaers): True

4.3.2 master

1.add()

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

print(s)
{1, 2, 3, 'a'}

2.remove()

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

print(s)
{2, 'a'}

3.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()

s = {1, 2, 'a'}
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

Guess you like

Origin www.cnblogs.com/tangceng/p/11304325.html