Python entry to proficiency_5 data containers (list, tuple, str, set, dict)


A data type that can hold multiple pieces of data. Each piece of data is called an element.
Each element can be any type of data, such as strings, numbers, Booleans, etc.

Data containers are divided into 5 categories according to different characteristics , such as:
whether it supports repeated elements
, whether it can be modified
, whether it is ordered, etc. , they are: list (list), tuple (tuple), string (str), set (set) , dictionary (dict)

1. list list

Each piece of data in the list is called an element, identified
by [ ]
, and each element in the list is separated by commas

1.1 Definition of list

basic grammar

# 定义变量
变量名称 = [元素1,元素2,元素3,....]

# 空列表
变量名称 = []
变量名称 = list()

There is no restriction on the data type of the elements, and even the elements can be lists, thus defining nested lists

my_list = [[1,2,3], [4,5,6]]
print(f"列表为:{
      
      my_list},数据类型为:{
      
      type(my_list)}")

insert image description here

注意:列表可以一次存储多个数据,且可以为不同的数据类型,支持嵌套

1.2 List subscript index

The direction from front to back, starting from 0, increasing in turn

We only need to follow the subscript index to get the element at the corresponding position.
Each element of the list has a number called a subscript index.
From the front to the back, the number increases from 0.
From the back to the front, the number decreases from -1.

my_list = [[1, 2, 3], [4, 5, 6]]
# 索引
print(f"内层第一个元素为:{
      
      my_list[0][0]}")

insert image description here

1.3 Summary of common operation methods

The list also provides a series of functions:
inserting elements,
deleting elements,
clearing the list ,
modifying elements
, counting the number of elements,
etc., we all call these functions: the method of the list

serial number How to use effect
1 list.append(element) append an element to the list (tail)
2 list.extend(container) Take out the contents of the data container in turn and append them to the end of the list
3 list.insert(subscript, element) Insert the specified element at the specified subscript
4 del list[subscript] Delete the specified subscript element of the list
5 list.pop(subscript) Delete the specified subscript element of the list
6 list.remove(element) From front to back, remove the first occurrence of this element
7 list.clear() clear the list
8 list.count(elements) counts the number of times this element occurs in the list
9 list.index(element) Find the specified element in the subscript of the list and cannot find an error ValueError
10 len(list) counts how many elements are in the container

The list has the following characteristics :
it can accommodate multiple elements (the upper limit is 2**63-1, 9223372036854775807),
it can accommodate different types of elements (mixed),
the data is stored in an orderly manner (with subscript serial numbers), and duplicate data is
allowed
Modify (add or remove elements, etc.)

the case

There is a list, the content is: [21, 25, 21, 23, 22, 20], which records the age of a group of students

Please use the function (method) of the list to
define the list and receive it with variables
Append a number 31 to the end of the list
Add a new list [29, 33, 30] to the end of the list
to take out the first element (Should be: 21)
Take out the last element (Should be: 30)
Find element 31, the subscript position in the list

# 案例
age = [21, 25, 21, 23, 22, 20]
# 追加31到尾部
age.append(31)
print(age)
# 追加一个新列表到尾部
age.extend([29,33,30])
print(age)
# 取出第一个元素
print(f"取出的第一个元素是:{
      
      age[0]}")
# 取最后一个元素
print(f"最后一个元素:{
      
      age[-1]}")
# 查找元素31
print(f"元素31的下标:{
      
      age.index(31)}")

insert image description here

1.4 List traversal for/while

Since the data container can store multiple elements, there will be a need to sequentially take out elements from the container for operation.
The behavior of taking out the elements in the container for processing in turn is called: traversal and iteration.

while loop

index = 0
while index < len(列表):
	元素 = 列表[index]
	方法
	index += 1

for loop

for 临时变量 in 数据容器:
	处理操作

Comparing
the while loop and the for loop, both are loop statements, but the details are different:
In terms of loop control:
the while loop can customize the loop conditions, and control the for loop by itself.
The for loop cannot customize the loop conditions, and can only take out data from the container one by one.
On infinite loops:
while loops can be controlled by conditions to achieve infinite loops
For loops are theoretically impossible, because the capacity of the traversed container is not infinite
In usage scenarios:
while loops are suitable for any scenario where you want to loop
For loops are suitable for , the scenario of traversing the data container or a simple fixed-time loop scenario

the case

Define a list, the content is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] traverse the list,
take out the even numbers in the list, and store them in a new list object
using while loop and The for loop operates once

# 练习案例
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# while 循环
new_list = []
index = 0
while index < len(my_list):
    if my_list[index] % 2 == 0:
        new_list.append(my_list[index])
    index += 1
print(new_list)

# for 循环
new_list = []
for i in my_list:
    if i % 2 == 0:
        new_list.append(i)
print(new_list)

insert image description here

2. tuple tuple

Like lists, tuples can encapsulate multiple elements of different types.
But the biggest difference is that
once the tuple is defined, it cannot be modified

2.1 Define tuples

Parentheses are used to define tuples, and commas are used to separate each data, and the data can be of different data types.

变量名称 = (元素1,元素2,...)
# 定义口元组
变量名称 = ()
变量名称 = tuple()

注意:元组只有一个数据,这个数据后面要添加逗号

2.2 Summary of related operations

serial number method effect
1 index() Find a certain data, if the data exists, return the corresponding subscript, otherwise report an error
2 count() Count the number of times a data appears in the current tuple
3 len(tuple) Count the number of elements in a tuple

元组由于不可修改的特性,所以其操作方法非常少。

Features:

After the above study of tuples, it can be concluded that the list has the following characteristics:
it can accommodate multiple data
, it can accommodate different types of data (mixed)
data is stored in an orderly manner (subscript index),
duplicate data is allowed
and cannot be modified ( Add or delete elements, etc.)
support for loop

Most of the features are the same as list, the difference lies in the unmodifiable features.

the case

Define a tuple, the content is: ('cxk', 2.5, ['football', 'music']), the records are (name, age, hobbies)

Please use the function (method) of the tuple to
query the subscript position of its age.
Query the name.
Delete the football in the hobbies
and add hobbies: coding to the hobbies list.

# 案例
my_tuple = ("cxk",11,["football","music"])
print(f"年龄的位置为:{
      
      my_tuple.index(11)}")
print(f"姓名:{
      
      my_tuple[0]}")
del my_tuple[2][0]
print(f"删除football后:{
      
      my_tuple}")
my_tuple[2].append("coding")
print(f"增加coding到list:{
      
      my_tuple}")

insert image description here

3. str string

Although strings don't look like lists and tuples, they look like containers that store a lot of data.
But it is undeniable that the string is also a member of the data container.

A string is a container of characters, and a string can store any number of characters.

3.1 Subscript index

Like other containers such as lists and tuples, strings can also be accessed through subscripts
. From front to back, subscripts start from 0
From back to front, subscripts start from -1

Like tuples, strings are one: unmodifiable data containers.
So:
modify the specified subscript characters (such as: string[0] = "a")
remove specific subscript characters (such as: del string[0], string.remove(), string.pop(), etc. )
to append characters (such as: string.append())
cannot be completed. If you have to do it, you can only get a new string, and the old string cannot be modified

3.2 Summary of common operations

serial number operate illustrate
1 string[subscript] Extract the character at a specific position according to the subscript index
2 string.index(string) Find the subscript of the first occurrence of the given character
3 string.replace(string1, string2) Replacing all string 1 in the string with string 2 will not modify the original string, but get a new one
4 string.split(string) According to the given string, delimiting the string will not modify the original string, but get a new list
5 string.strip() string.strip(string) Remove leading and trailing spaces and newlines or specify a string
6 string.count(string) Count the number of occurrences of a string in a string
7 len(string) Count the number of characters in a string

Find the subscript index value of a specific string

 语法:字符串.index(字符串)

string replacement

语法:字符串.replace(字符串1,字符串2
功能:将字符串内的全部:字符串1,替换为字符串2
注意:不是修改字符串本身,而是得到了一个新字符串

string splitting

语法:字符串.split(分隔符字符串)
功能:按照指定的分隔符字符串,将字符串划分为多个字符串,并存入列表对象中
注意:字符串本身不变,而是得到了一个列表对象

string remove spaces

语法:字符串.strip()  #去除两端空格

count the number of occurrences of a certain string in the count string count
the length of the count string len

String Traversal

Like lists and tuples, strings also support while loops and for loops for traversal

3.3 Characteristics of strings

As a data container, strings have the following characteristics:
only strings can be stored
, the length is arbitrary (depending on the memory size),
subscript
indexes are supported, duplicate strings are allowed,
no modification (adding or deleting elements, etc.) is
supported, and for loops are supported

Basically the same as lists and tuples. The difference
from lists and tuples is that the type that a string container can hold is a single type, which can only be a string type.
Unlike lists, the same as tuples: strings cannot be modified

the case

Given a string: "itcsdn itcast sb"
Count how many "it" characters are in the string
Replace all spaces in the string with characters: "|"
and split the string according to "|"

my_list = "itcsdn itcast sb"
print(f"有{
      
      my_list.count('it')}个it字符")
new_list = my_list.replace(" ", "|")
print(f"替换|后的字符串为:{
      
      new_list}")
new1_list = my_list.split("|")
print(f"按|分割字符串为:{
      
      new1_list}")

insert image description here

4. Slicing of sequences

Sequence refers to: a type of data container whose content is continuous and ordered, and which can be indexed by subscripts. The
列表、元组、字符串,均可以可以视为序列。
typical characteristics of sequences are: ordered and indexed by subscripts. Strings, tuples, and lists all meet this requirement
. , take out a subsequence

序列[起始下标:结束下标:步长]
# 表示从序列中,从指定位置开始,依次取出元素,到指定位置结束,得到一个新序列

The start subscript indicates where to start, and it can be left blank. If it is left blank, it is regarded as starting from the beginning. The end
subscript (excluding) indicates where to end, and it can be left blank. If it is left blank, it is regarded as intercepting to the end
step, and the elements are taken in turn. Interval
Step 1 indicates that elements are taken one by one.
Step 2 indicates that one element is skipped each time.
Step N indicates that N-1 elements are skipped each time. The step
is negative, and the reverse is taken (note , the start subscript and end subscript should also be reversed)
注意,此操作不会影响序列本身,而是会得到一个新的序列(列表、元组、字符串)

Sequence slicing actual combat
There is a string: "The error of the work is not suitable, the life is too short"
use the slice to get "the life is too short"

my_str1 = "作工的误错做合适不,短太命生"
new_str1 = my_str1[-1:-5:-1]
print(f"切片字符串为:{
      
      new_str1}")

insert image description here

5. set collection

The main feature of the collection is that it does not support the repetition of elements ( it comes with deduplication function ), and the content is out of order

# 定义集合变量
定义集合变量 = {
    
    元素,元素,....,元素}
# 定义空集合
变量名称 = set()

It is basically the same as the definition of list, tuple, string, etc.
list uses: []
tuple uses: ()
string uses: ""
set uses: {}

5.1 Common operations on collections - modification

First of all, because the collection is unordered, the collection does not support: subscript index access
But the collection, like the list, is allowed to be modified

add new element

# 语法
集合.add(元素) #将指定元素,添加到集合内
# 结果:集合本身被修改,添加了新元素

remove element

# 语法
集合.remove(元素) # 将指定元素,从集合内移除
# 结果:集合本身被修改,移除了元素

Randomly remove elements from a collection

# 语法
集合.pop() #功能,从集合中随机取出一个元素
# 结果:会得到一个元素的结果。同时集合本身被修改,元素被移除

empty collection

# 语法
集合.clear() # 功能,清空集合
# 结果:集合本身被清空

5.3 Common operations on collections - two collections

Take the difference of 2 sets

# 语法
集合1.difference(集合2) # 功能:取出集合1和集合2的差集(集合1有而集合2没有的)
# 结果:得到一个新集合,集合1和集合2不变

eg

# 取两个集合的差集difference
set1 = {
    
    1,3,5,7,9}
set2 = {
    
    2, 4, 5, 7, 9}
set3 = set1.difference(set2)
print(f"两个集合的差集为:{
      
      set3}")
print(f"原集合set1:{
      
      set1}")
print(f"原集合set2:{
      
      set2}")

insert image description here

Eliminate the difference of 2 sets

# 语法
集合1.difference_update(集合2) #功能:对比集合1和集合2,在集合1内,删除和集合2相同的元素。
# 结果:集合1被修改,集合2不变
set1 = {
    
    1,3,5,7,9}
set2 = {
    
    2, 4, 5, 7, 9}
set1.difference_update(set2)
print(f"消除两个集合的差集为:{
      
      set1}")
print(f"原集合set2:{
      
      set2}")

insert image description here

2 collections merged

语法:集合1.union(集合2)
功能:将集合1和集合2组合成新集合
结果:得到新集合,集合1和集合2不变
set1 = {
    
    1,3,5,7,9}
set2 = {
    
    2, 4, 5, 7, 9}
set3 = set1.union(set2)
print(f"两个集合为:{
      
      set3}")
print(f"原集合set1:{
      
      set1}")
print(f"原集合set2:{
      
      set2}")

insert image description here

5.2 Common operations on collections - collection length

# 查看集合的元素数量
语法:len(集合)
功能:统计集合内有多少元素
结果:得到一个整数结果

5.3 Common operations on collections - for loop traversal

The collection also supports the use of for loop traversal (syntax as above)
the collection is unordered
要注意:集合不支持下标索引,所以也就不支持使用while循环。

5.4 Summary of common functions of collection

serial number operate illustrate
1 collection.add(element) add an element to the set
2 collection.remove(element) removes the specified element from the collection
3 collection.pop() Randomly select an element from a collection
4 Set.clear() empty the collection
5 set1.difference(set2) Get a new set, which contains the difference set of 2 sets, and the contents of the original 2 sets remain unchanged
6 set1.difference_update(set2) In set 1, delete elements present in set 2 set 1 is modified, set 2 is unchanged
7 Collection 1.union(collection 2) Get a new collection, containing all the elements of the two collections, the contents of the original two collections remain unchanged
8 len(collection) Get an integer that records the number of elements in the collection

After the above understanding of collections, it can be concluded that collections have the following characteristics :

  • Can hold multiple data
  • Can accommodate different types of data (mixed)
  • Data is stored out of order (subscript indexes are not supported)
  • Duplicate data is not allowed
  • Can be modified (add or remove elements, etc.)
  • Support for loop

the case

There are the following list objects:
my_list = ['I', 'Yes', 'Xiao Fang Cai', 'Xiao Party Cai', 'cai', 'cai', 'da', 'sa', 'bi']

请:
定义一个空集合
通过for循环遍历列表
在for循环中将列表的元素添加至集合
最终得到元素去重后的集合对象,并打印输出

## 案例
my_set = set()
my_list = ['我', '是', '小趴菜', '小趴菜', 'cai', 'cai', 'da', 'sa', 'bi']
for i in my_list:
    my_set.add(i)

print(my_set)

insert image description here

6. dict字典

为什么使用字典?
因为可以使用字典,实现用key取出Value的操作

6.1 字典的定义

字典的定义,同样使用{},不过存储的元素是一个个的:键值对,如下语法:

# 定义字典变量
my_dict = {
    
    key: value}
# 定义空字典
my_dict = {
    
    }
my_dict = dict()

  • 使用{}存储原始,每一个元素是一个键值对
  • 每一个键值对包含Key和Value(用冒号分隔)
  • 键值对之间使用逗号分隔
  • Key和Value可以是任意类型的数据(key不可为字典)
  • Key不可重复,重复会对原有数据覆盖

6.2 字典的嵌套及获取

获取

字典同集合一样,不可以使用下标索引
但是字典可以通过Key值来取得对应的Value

嵌套

字典的Key和Value可以是任意数据类型(Key不可为字典)
那么,就表明,字典是可以嵌套的

需求如下:记录学生各科的考试信息

姓名 语文 数学 英语
小王 60 70 80
小趴 78 80 90
小菜 66 77 88
## 记录学生成绩
stu_score = {
    
    
    "小王": {
    
    '语文':60, "数学":70, "英语":80},
    "小趴": {
    
    '语文':78, "数学":80, "英语":90},
    "小菜": {
    
    '语文': 66, "数学": 77, "英语": 88}
}

# 获取值
print(f"小王的成绩为:{
      
      stu_score['小王']}")
print(f"小趴的语文成绩为:{
      
      stu_score['小趴']['语文']}")

insert image description here

6.3 字典的常用操作

  • 新增元素

语法:字典[Key] = Value
结果:字典被修改,新增了元素

stu_score = {
    
    
    "小王": {
    
    '语文':60, "数学":70, "英语":80},
    "小趴": {
    
    '语文':78, "数学":80, "英语":90},
    "小菜": {
    
    '语文': 66, "数学": 77, "英语": 88}
}
# 新增
stu_score["大菜"] = {
    
    '语文': 66, "数学": 77, "英语": 88}
print(f"新增后:{
      
      stu_score}")

insert image description here

  • 更新元素

语法:字典[Key] = Value,结果:字典被修改,元素被更新
注意:字典Key不可以重复,所以对已存在的Key执行上述操作,就是更新Value值

stu_score = {
    
    
    "小王": {
    
    '语文':60, "数学":70, "英语":80},
    "小趴": {
    
    '语文':78, "数学":80, "英语":90},
    "小菜": {
    
    '语文': 66, "数学": 77, "英语": 88}
}
# 更新
stu_score["小菜"] = {
    
    '语文': 67, "数学": 78, "英语": 89}
print(f"更新后:{
      
      stu_score}")

insert image description here

  • 删除元素

语法:字典.pop(Key)
结果:获得指定Key的Value,同时字典被修改,指定Key的数据被删除

stu_score = {
    
    
    "小王": {
    
    '语文':60, "数学":70, "英语":80},
    "小趴": {
    
    '语文':78, "数学":80, "英语":90},
    "小菜": {
    
    '语文': 66, "数学": 77, "英语": 88}
}

# 删除pop
value = stu_score.pop("小王")
print(f"删除的值:{
      
      value}")
print(f"删除后的原数据:{
      
      stu_score}")

insert image description here

  • 清空字典

语法:字典.clear()
结果:字典被修改,元素被清空

stu_score = {
    
    
    "小王": {
    
    '语文':60, "数学":70, "英语":80},
    "小趴": {
    
    '语文':78, "数学":80, "英语":90},
    "小菜": {
    
    '语文': 66, "数学": 77, "英语": 88}
}
# 清除字典
stu_score.clear()
print(f"清除后:{
      
      stu_score}")

insert image description here

  • 获取全部的key

语法:字典.keys()
结果:得到字典中的全部Key

stu_score = {
    
    
    "小王": {
    
    '语文':60, "数学":70, "英语":80},
    "小趴": {
    
    '语文':78, "数学":80, "英语":90},
    "小菜": {
    
    '语文': 66, "数学": 77, "英语": 88}
}

keys = stu_score.keys()
print(f"所有的键为:{
      
      keys}")

insert image description here

  • 遍历字典

语法:for key in 字典.keys()

stu_score = {
    
    
    "小王": {
    
    '语文':60, "数学":70, "英语":80},
    "小趴": {
    
    '语文':78, "数学":80, "英语":90},
    "小菜": {
    
    '语文': 66, "数学": 77, "英语": 88}
}

keys = stu_score.keys()
for key in keys:

    print(f"学生:{
      
      key}, 分数:{
      
      stu_score[key]}")

insert image description here
注意:字典不支持下标索引,所以同样不可以用while循环遍历

  • 计算字典内的全部元素(键值对)数量

语法:len(字典)
结果:得到一个整数,表示字典内元素(键值对)的数量

6.4 字典的常用操作总结

编号 操作 说明
1 字典[Key] 获取指定Key对应的Value值
2 字典[Key] = Value 添加或更新键值对
3 字典.pop(Key) 取出Key对应的Value并在字典内删除此Key的键值对
4 字典.clear() 清空字典
5 字典.keys() 获取字典的全部Key,可用于for循环遍历字典
6 len(字典) 计算字典内的元素数量

案例

有如下员工信息,请使用字典完成数据的记录。
并通过for循环,对所有级别为1级的员工,级别上升1级,薪水增加1000元

姓名 部门 工资 级别
小王 科技部 8000 1
小趴 市场部 6000 2
小菜 人事部 7000 1
大趴 市场部 4000 3
大菜 科技部 5000 2
# 组织字典记录数据
info_dict = {
    
    
    "小王": {
    
    
        "部门": "科技部","工资": 8000,"级别": 1
    },
    "小趴": {
    
    
        "部门": "市场部","工资": 6000,"级别": 2
    },
    "小菜": {
    
    
        "部门": "人事部","工资": 7000,"级别": 1
    },
    "大趴": {
    
    
        "部门": "市场部","工资": 4000,"级别": 3
    },
    "大菜": {
    
    
        "部门": "科技部","工资": 5000,"级别": 2
    }
}

print(f"员工在升值加薪之前的结果:{
      
      info_dict}")

# for循环遍历字典
for name in info_dict:
    # if条件判断符合条件员工
    if info_dict[name]["级别"] == 1:
        # 升职加薪操作
        # 获取到员工的信息字典
        employee_info_dict = info_dict[name]
        # 修改员工的信息
        employee_info_dict["级别"] = 2    # 级别+1
        employee_info_dict["工资"] += 1000    # 工资+1000
        # 将员工的信息更新回info_dict
        info_dict[name] = employee_info_dict

# 输出结果
        print(f"对员工进行升级加薪后的结果是:{
      
      info_dict}")

insert image description here

7. 总结

7.1 数据容器分类

数据容器可以从以下视角进行简单的分类:

是否支持下标索引

  • 支持:列表、元组、字符串 - 序列类型
  • 不支持:集合、字典 - 非序列类型

是否支持重复元素

  • 支持:列表、元组、字符串 - 序列类型
  • 不支持:集合、字典 - 非序列类型

是否可以修改

  • 支持:列表、集合、字典
  • 不支持:元组、字符串
列表 元组 字符串 集合 字典
元素数量 支持多个 支持多个 支持多个 支持多个 支持多个
元素类型 任意 任意 仅字符 任意 Key:ValueKey:除字典外任意类型Value:任意类型
下标索引 支持 支持 支持 不支持 不支持
重复元素 支持 支持 支持 不支持 不支持
可修改性 支持 不支持 不支持 支持 支持
数据有序
使用场景 可修改、可重复的一批数据记录场景 不可修改、可重复的一批数据记录场景 一串字符的记录场景 不可重复的数据记录场景 以Key检索Value的数据记录场景

7.2 容器通用功能

功能 描述
通用for循环 遍历容器(字典是遍历key)
max 容器内最大元素
min() 容器内最小元素
len() 容器元素个数
list() 转换为列表
tuple() 转换为元组
str() 转换为字符串
set() 转换为集合
sorted(sequence, [reverse=True]) Sorting, reverse=True means descending order to get a sorted list

Guess you like

Origin blog.csdn.net/hexiaosi_/article/details/127577839