20190805 container data type built-in method

List of data types built-in method

  1. effect

    Description plurality of values, such as hobbies

  2. Defined way

    hobby_list = ['play', 'swimming', 'dancing', '666']

  3. Built-in method

    Priority grasp

    1. Index values

         1. hobby_list = ['play', 'swimming', 'dancing', '666']
      
        print(1, hobby_list[-1])
        hobby_list[-1] = '233'  # 索引修改
        print(2, hobby_list[-1])
    2. slice

       print(hobby_list[:])  # ['play', 'swimming', 'dancing', '666']
       print(hobby_list[::-1])  # 从右到左  # ['666', 'dancing', 'swimming', 'play']
    3. length

      print(len(hobby_list))
    4. in/not in

      hobby_list = ['play', 'swimming', 'dancing', '666']
      print('play' in hobby_list)  # True
    5. for loop

      for hobby in hobby_list:
          print(hobby)
    6. del Delete

      del hobby_list[-1]
      print(hobby_list)
    7. append () # add

      hobby_list = ['play', 'swimming', 'dancing', '666']
      hobby_list.append('dapao-->piao')
      print(hobby_list)

Need to know

  1. count count

       hobby_list = ['play', 'swimming', 'dancing', '666', 666, '666']
       print(hobby_list.count('666'))  
    
  2. extend an expanded list

   hobby_list.extend([1, 2, 3, 4])  
   print(hobby_list)
  1. clear clear
   hobby_list.clear()  
   print(hobby_list)
  1. copy Copy List
   hobby_list2 = hobby_list.copy() 
       hobby_list[-1] = '233'
   print(hobby_list)
   print(hobby_list2)
  1. pop delete, delete the last default
   hobby_list.pop()  
   print(hobby_list)
   
   hobby_list = ['play', 'swimming', 'dancing', 'play', '666', 666, '666']
   print(hobby_list.index('play'))  # 索引
   print(hobby_list.index('play', 2, 4))  # 索引2-4内有没有该值
   
  1. insert insert

    hobby_list.insert(0, '1')  
    print(hobby_list)
  2. remove remove

    hobby_list.remove('1')
    print(hobby_list)
  3. reverse reverse

hobby_list.reverse()
print(hobby_list)
hobby_list = [1,2,434,5435,0,2304]
   
hobby_list = [['nick', 1000], ['jason', 500000], ['sean', 2000], ['tank', 10000]]
   
   
def func(i):  # ['nick', 1000]
     return i[1]  # 1000,500000,2000,10000
   
hobby_list.sort(key=func)  
hobby_list.sort(key=lambda i: i[1], reverse=True)  #print(hobby_list)
  1. A plurality of stored value or values

    Multiple values

  2. Ordered or unordered

    Ordered

  3. Variable or non-variable (Key)

    variable

Tuple data type built-in method

  1. Define how
    the list in brackets into a small parenthesis

    lis = [1,2,3,4]
    tup = (1,2,3,4)

    Ganso and a list of exactly the same, but can not be modified Ganso, Ganso defining moment of his value as well as the number of elements of elements of all fixed up

    print(tup[1])
    
    lis[1] = 5
    print(lis)
    tup[1] = 5
    print(tup)
    
    lis.append(5)
    print(lis)
    tup.

    Early forever it is generally used to reduce the memory footprint, now useless, since as long as the definition of the list on the line

    tup = (1,2,3,4)
    tup = tuple('lksjdkflj')
    print(tup.index(1))
    
    print(tup.count(1))
    1. A plurality of values stored value or
      a plurality of values
    2. Orderly or disorderly
      orderly
    3. Variable or non-variable (Key)
      has not even say this

Dictionary data type built-in method

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}
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是列表,还是字典,并且key不再具有描述信息
dic[0]: b
dic = {[1,2]: 'a', 0: 'b'}  # 报错

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.

1.1 Priority control (* * *)

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

1.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

dic = {'a': 1, 'b': 2}
print(f'first:{id(name)}')
dic['a'] = 3
print(f'second:{id(name)}')
first:4356627632
second:4356627632

6. Variable or immutable: Variable Data Type

A set of built-in data types Method

Collection can be understood as a collection of learning Python is a collection of students; students can learn Linux is a collection.

pythoners = ['jason', 'nick', 'tank', 'sean']
linuxers = ['nick', 'egon', 'kevin']

# 即报名pythoners又报名linux的学生
py_li_list = []
for stu in pythoners:
    if stu in linuxers:
        py_li_list.append(stu)
print(f"pythoners and linuxers: {py_li_list}")
pythoners and linuxers: ['nick']

The above list of ways aggregate sum of two relational operators are very complex, so we have a set of data types.

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.

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

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.

1.1 Priority control (* * *)

  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

Collection type built-in arithmetic -? Set operation English .jpg x-oss-process = style / watermark

3. | union

# str之|并集
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

# str之&交集
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

# str之-差集
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 ^

# str之^对称差集
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.==

# str之==
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:>,> =

# str之父集:>、>=
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: <, <=

# str之子集:<、<=
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

1.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

s = {1, 2, 'a'}
print(f'first:{id(s)}')
s.add(3)
print(f'second:{id(s)}')
first:4480523848
second:4480523848

6. Variable or immutable: Variable Data Type

Guess you like

Origin www.cnblogs.com/TMesh-python/p/11304833.html