python---list, tuple, dictionary, set

Knowledge point one: list type

           1.Definition

<class 'list'>

             1) Ordered container data and variable data types

             2) Function: Store multiple values ​​to facilitate data extraction in order, such as storing some names

# 姓名列表
name_list=['Simon','Andy','Mark','Eric','Jicky']
# 一个列表可以存放各种类型的数据
l=['Simon',56,2.3,[1,2,3],{'Andy':45}]

          2.Method

             1) increase

                   a) Add elements to the end of the list: append() method

numList=[1,4,6,4,2,5,8,3]
numList.append(0)
print(numList)

                    b) Insert elements in the middle of the list: insert() method

numList=[1,4,6,4,2,5,8,3]
numList.insert(3,44) #在索引3的位置插入元素44
print(numList)

                     c) Add multiple elements to the end of the list at once and put the list in parentheses: extend() method

numList=[1,4,6,4,2,5,8,3]
numList.extend([2,3,4]) # 在列表末尾添加一个列表[2,3,4]
print(numList)

                      d) Adding two lists can also be done

numList=[1,4,6,4,2,5,8,3]
name_list=['Simon','Andy','Mark','Eric','Jicky']
newList=numList+name_list
print(newList)

                2) Delete

                     a) Delete the last element and return the deleted element: pop() method

numList=[1,4,6,4,2,5,8,3]
num=numList.pop() #删除最后一个元素3
print(numList)
print(num)

                     b) Delete according to element index: pop (index) method, del () function

numList=[1,4,6,4,2,5,8,3]
num=numList.pop(3) #删除索引为3的元素4
print(numList)
print(num)
del(numList[2]) #删除numList列表中索引为2 的元素
print(numList)

                      c) Delete according to the element name: remove(element name) method

numList=[1,4,6,4,2,5,8,3]
numList.remove(4) #删除元素4(从左往右删除第一个)
print(numList

                      d) Clear the list: clear() method

numList=[1,4,6,4,2,5,8,3]
numList.clear() # 清空numList列表
print(numList)

                 3) Check

                      a) Index: The index refers to the position of the element in the list, starting from 0

numList=[1,4,6,4,2,5,8,3]
print(numList[2]) # 从左到右,从0开始
print(numList[-1]) # -1表示最后一个

                      b) Query element index: index() method

numList=[1,4,6,4,2,5,8,3]
print(numList.index(4)) #有就返回第一个索引
print(numList.index(34)) #没有就报错

                      c) Slicing: intercept a section of the list (regardless of the beginning and end)

numList=[1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4]
print(numList[2:5]) # 索引2到4的元素
print(numList[:3]) # 从左到索引2的位置
print(numList[2:]) # 从索引2到最后
print(numList[2:10:2]) #从2到9,步长为2

                       d) Query list length: len() function

numList=[1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4]
print(len(numList))

                       e) Find the maximum value: maximum value max() function minimum value min() function

numList=[1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4]
print(max(numList))
print(min(numList))

                        f) Member function in not in determines whether the element is in the list

numList=[1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4]
print(2 in numList)
print(4 not in numList)

                   4) Change

                         a) Direct assignment

numList=[1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4]
numList[3]=123 #将索引3的元素改为123
print(numList)

                         b) Reverse order of list: reverse() method

numList=[1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4]
numList.reverse()
print(numList)

                          c) List sorting: sort() method, sorted() function

numList=[1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4]
numList.sort() # 直接将原列表排序
print(numList)
newList=sorted(numList) # 将列表元素排序,返回排序结果不改变原列表
print(numList)
print(newList)

Knowledge point 2: Tuple type

           1.Definition

<class 'tuple'>

              1) Ordered immutable container data type

              2) Used to store multiple data that does not need to be changed. Its usage is similar to that of a list, but it is immutable.

           2.Method

##Tuples are immutable types, so they cannot be deleted or added, but tuples can be extracted and spliced ​​by addition.

t1=(1,2,3)
t2=(2,3,4)
t3=t1+t2
print(t3)

##The query method for tuples is the same as for lists

                1) Index: The index refers to the position of the element in the tuple, starting from 0.

numTuple=(1,4,6,4,2,5,8,3)
print(numTuple[2]) # 从左到右,从0开始
print(numTuple[-1]) # -1表示最后一个

                 2) Query element index: index() method

numTuple=(1,4,6,4,2,5,8,3)
print(numTuple.index(4)) #有就返回第一个索引
print(numTuple.index(34)) #没有就报错

                 3) Slicing: intercept a section of the list (regardless of the beginning and end)

numTuple=(1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4)
print(numTuple[2:5]) # 索引2到4的元素
print(numTuple[:3]) # 从左到索引2的位置
print(numTuple[2:]) # 从索引2到最后
print(numTuple[2:10:2]) #从2到9,步长为2

                  4) Query list length: len() function

numTuple=(1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4)
print(len(numTuple))

                   5) Find the maximum value: maximum value max() function minimum value min() function

numTuple=(1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4)
print(max(numTuple))
print(min(numTuple))

                    6) Member function in not in determines whether the element is in the tuple

numTuple=(1,4,6,4,2,5,8,3,4,2,34,32,3,765,46,4)
print(2 in numTuple)
print(4 not in numTuple)

## Tuples are immutable types and cannot be changed normally. However, in some special cases, some methods can be used to change the tuples.

                    7) Convert tuples into lists for changes and additions

numTuple=(1,2,3,4,5,6,7)
numList=list(numTuple) # 将元组转换成列表
numList.append(8) # 为列表添加元素
numTuple=tuple(numList) # 将列表转换成元组
print(numTuple)

                     8) The list within the tuple can be changed

numTuple=(1,2,3,4,5,6,7,[1,2,3]) # 元组内包含列表
numTuple[7][1]='python' # 更改列表里面的元素
print(numTuple)

Knowledge point 3: Dictionary type

           1.Definition

<class 'dict'>

             1) Definition: An unordered variable container data type that stores multiple key-value pairs

             2) Function: Data stored in the form of key:value pairs. For example, a person’s basic information (name, age, home address, school, etc.)

             3) The key in the dictionary must be the only immutable type, and the value can be any data type.

             4) Therefore, the key in the dictionary cannot be repeated, and the value can be repeated.

# 按照现实中字典去理解,键key理解为一个汉字,值value理解为这个汉字所在的页码
# 一个汉字只有一个页码(不考虑多音字),一个页码可以对应多个汉字
name_dict={'刘':33,'王':45,'章':53,'张':53,'李':43}
print(type(name_dict))

         2.Method

            1) increase

                  a) Just assign a key that does not exist directly

name_dic = {'刘': 33, '王': 45, '章': 53, '张': 53, '李': 43}
name_dic['赵']=55
print(name_dic)

             2) Delete

                   a) Clear the dictionary: clear() method

name_dic = {'刘': 33, '王': 45, '章': 53, '张': 53, '李': 43}
name_dic.clear()
print(name_dic)

                   b) Directly delete the keys of the dictionary: del() function

name_dic = {'刘': 33, '王': 45, '章': 53, '张': 53, '李': 43}
del(name_dic['王'])
print(name_dic)
# 删除键之后对应的值也就删除了

                   c) Take out the key-value pairs in the dictionary and use them: pop() method

name_dic = {'刘': 33, '王': 45, '章': 53, '张': 53, '李': 43}
name=name_dic.pop('刘')
print(name_dic)
print(name) # 删除字典中键值对,并返回键对应的值

                   d) Take out the last key-value pair displayed in the dictionary: popitem() method

name_dic = {'刘': 33, '王': 45, '章': 53, '张': 53, '李': 43}
name=name_dic.popitem() #取出最后一个键值对
print(name_dic)
print(name) # 返回键值对的元组

## Note: The dictionary is an unordered type, so the popitem() method deletes the last one in the order displayed by the compiler. In fact, there is no order inside the dictionary.

             3) Check

                   a) Search value by key: directly search the key

Simon={'name':'Simon','age':29,'height':175}
print(Simon['age']) # 有键就返回对应的值
print(Simon['weight']) # 无键就报错

                    b) Find value by key: get() method

Simon={'name':'Simon','age':29,'height':175}
print(Simon.get('age')) # 有键就返回对应的值
print(Simon.get('weight')) # 无键就返回None

                     c) Remove all keys: keys() method

Simon={'name':'Simon','age':29,'height':175}
print(Simon.keys())

                       d) Take out all values ​​value: values() method

Simon={'name':'Simon','age':29,'height':175}
print(Simon.values())

                        e) Take out all key-value pairs: items() method

Simon={'name':'Simon','age':29,'height':175}
print(Simon.items())

                          f) Member operation: in not in

Simon={'name':'Simon','age':29,'height':175}
print('name' in Simon)
print('hobby'not in Simon)

                    4) Change

                          a) Reassign directly

Simon={'name':'Simon','age':29,'height':175}
Simon['age']=30
print(Simon)

                          b) Update dictionary: update() method

Simon={'name':'Simon','age':29,'height':175}
other={'hobby':'python','address':'JiangSu'}
Simon2={'age':30,'height':175}
Simon.update(other) # 原字典没有的键,直接添加进去
print(Simon)
Simon.update(Simon2)# 原字典已有的键,修改新的值
print(Simon)

                          c) Set default value: setdefault() method

Simon={'name':'Simon','age':29,'height':175}
hobby=Simon.setdefault('hobby','python') # 原字典没有的键,直接添加进去,返回新值
print(Simon,hobby)
name=Simon.setdefault('name','Andy') # 原字典已有的键,不改变原值,返回原值
print(Simon,name)

Knowledge point 4: Collection type

           1.Definition

<class 'set'>

             1) Unordered non-repeating variable type data

             2) Function: often used for relational operations

name_set={'Simon','Andy','Mark','Eric','Jackie'}
# 集合中元素不能重复且都为不可变类型

          2.Method

             1) increase

name_set={'Simon','Andy','Mark','Eric','Jackie'}
name_set.add('James')
print(name_set)

             2) Delete

                   a) Delete the first one displayed: pop() method

name_set={'Simon','Andy','Mark','Eric','Jackie'}
print(name_set)
name_set.pop()
print(name_set)

        ##Note: Since the order of the collection displayed is different each time, the object deleted by pop() is uncertain.  

                   b) Deletion of specified elements: remove() method

name_set={'Simon','Andy','Mark','Eric','Jackie'}
print(name_set)
name_set.remove('Andy')
print(name_set)

              3) Change

 

name_set={'Simon','Andy','Mark','Eric','Jackie'}
name_set.update(['Simon','Coco'])
print(name_set)

        3. Relational operations

           1) Intersection

set1={1,2,3,4,5,6,7,8}
set2={4,5,6,7,8,9,10}
print(set1&set2) # 得到两个集合共同部分

          2) Union

set1={1,2,3,4,5,6,7,8}
set2={4,5,6,7,8,9,10}
print(set1|set2) # 得到两个集合全部数据(去重)

         3) Difference set

set1={1,2,3,4,5,6,7,8}
set2={4,5,6,7,8,9,10}
print(set1-set2) # 除去set1与set2重复的数据后,set1剩余的数据

         4) Deduplication

##Because sets can perform relational operations, they can be used to perform list deduplication operations.

##Disadvantages: The order of the original data cannot be guaranteed, and the original data must all be immutable types.

l=[1,2,4,5,34,21,2,1,5,4,2,1,1,3,4,5,6,3,2,3]
s=set(l)
l=list(s)
print(l)

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/127708873