Python 基础(三)列表、元组、集合与字典

一、环境

环境 版本
操作系统 CentOS 7.9.2009
Python 3.9.7

二、列表

列表是 Python 中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。

列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。

列表的数据项不需要具有相同的类型

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]

2.1 列表取值

脚本 2.1.py

#!/usr/bin/env python3

courses = ['Linux', 'Python', 'Vim', 'C++']

# 取第一个值
print(courses[0])

# 取最后一个值
print(courses[-1])

# 取倒数第二个值
print(courses[-2])
[root@localhost 3]# python3 2.1.py 
Linux
C++
Vim

2.2 列表操作

# 定义两个列表
>>> courses = ['Linux', 'Python', 'Vim', 'C++' ]
>>> score  = [ 99 , 89 ,70 , 59 , 89 ]

# 末尾追加一个元素
>>> courses.append('PHP')
>>> courses
['Linux', 'Python', 'Vim', 'C++', 'PHP']

# 根据索引位置插入一个元素
>>> courses.insert(0,'Ruby')
>>> courses
['Ruby', 'Linux', 'Python', 'Vim', 'C++', 'PHP']

# 统计列表元某个素数出现的次数
>>> score.count(89)
2

# 移除任意指定值
>>> courses.remove('Linux')
>>> courses
['Ruby', 'Python', 'Vim', 'C++', 'PHP']

# 元素排序
>>> score.sort()
>>> score
[59, 70, 89, 89, 99]

# 反转元素顺序
>>> courses.reverse()
>>> courses
['PHP', 'C++', 'Vim', 'Python', 'Ruby']

# 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
>>> courses.pop(-2)
'Python'

# 从列表中找出某个值第一个匹配项的索引位置
>>> score.index(89)
2

# 列表元素个数
>>> len(courses)
4

# 返回列表元素最大值
>>> max(score)
99

# 返回列表元素最小值
>>> min(score)
59

三、元祖

tuple(元组)是一种特殊的列表,不同点是元组一旦创建就不能修改。

元组使用小括号,列表使用方括号。

脚本 3.py

#!/usr/bin/env python3

courses = ('Linux', 'Python', 'Vim', 'C++')

# 取第一个值
print(courses[0])

# 取最后一个值
print(courses[-1])

# 取倒数第二个值
print(courses[-2])
[root@localhost 3]# python3 3.py 
Linux
C++
Vim

四、集合

set(集合)是一个无序不重复元素的数据集,对比列表的区别首先是无序的,不可以使用索引进行顺序的访问,另外一个特点是不能够有重复的数据。

项目开发中,集合主要用在数据元素的去重和测试是否存在。集合还支持一些数学上的运算,例如:union(联合),intersection(交),difference(差)和 symmetric difference(对称差集)。

脚本 4.1.py

#!/usr/bin/env python3

courses = set()
print(type(courses))

courses = {
    
    'Linux', 'C++', 'Vim', 'Linux'}
print(courses)
[root@localhost 3]# python3 4.py 
<class 'set'>
{
    
    'Linux', 'Vim', 'C++'}

集合还可以直接由字符串与 set 函数进行创建,会将字符串拆散为不同的字符,并去除重复的字符

脚本 4.2.py

#!/usr/bin/env python3

nameset = set('hello,word')
print(nameset)
[root@localhost 3]# python3 4.2.py 
{
    
    'h', 'e', 'l', 'w', 'r', 'd', ',', 'o'}

4.1 集合操作

脚本 4.3.py

# 定义一个集合
>>> courses = set()
>>> courses = {
    
    'Linux', 'C++', 'Vim', 'Linux'}

# 判断元素是否存在集合中
>>> 'Linux' in courses
True
>>> 'Python' in courses
False
>>> 'Python' not in courses
True

# 向集合中增加元素
>>> courses.add('Python')
>>> courses
{
    
    'Vim', 'C++', 'Python', 'Linux'}

# 从集合中删除元素
>>> courses.remove('Linux')
>>> courses
{
    
    'Vim', 'C++', 'Python'}

4.2 集合运算

脚本 4.4.py

# 定义两个集合
>>> set1 = {
    
    1,2,3,4}
>>> set2 = {
    
    3,4,5,6}

# 并集
>>> set1 | set2
{
    
    1, 2, 3, 4, 5, 6}

# 交集
>>> set1 & set2
{
    
    3, 4}

# 差集
>>> set1 - set2
{
    
    1, 2}
>>> set2 - set1
{
    
    5, 6}

# 返回只存在两个集合中的元素
>>> set1 ^ set2
{
    
    1, 2, 5, 6}

五、字典

dict(字典)是无序的键值对集合。字典中的每一个元素都是一个 key 和 一个 value 的组合,key 值在字典中必须是唯一的,因此可以很方便的从字典中使用 key 获得其对应的 value 的值。

脚本 5.1.py

#!/usr/bin/env python3

coursesdict = {
    
    1:'Linux', 2:'Vim'}
testdict = {
    
    1:2, 'teststr':'shiyanlou.com', 9:[1,2,3]}

print(coursesdict[1])
print(coursesdict[2])
print(testdict[9])
[root@localhost 3]# python3 5.1.py 
Linux
Vim
[1, 2, 3]

5.1 字典操作

脚本 5.2.py

#!/usr/bin/env python3

coursesdict = {
    
    1:'Linux', 2:'Vim'}

# 增加元素
coursesdict[5] = 'Bash'
coursesdict['c'] = 'Python'
print(coursesdict)

# 删除元素
del coursesdict[1]
print(coursesdict)

# 遍历字典
for key,value in coursesdict.items():
    print(key,value)

# 获取所有key值
print(coursesdict.keys())

# 获取所有value值
print(coursesdict.values())
[root@localhost 3]# python3 5.2.py 
{
    
    1: 'Linux', 2: 'Vim', 5: 'Bash', 'c': 'Python'}
{
    
    2: 'Vim', 5: 'Bash', 'c': 'Python'}
2 Vim
5 Bash
c Python
dict_keys([2, 5, 'c'])
dict_values(['Vim', 'Bash', 'Python'])

猜你喜欢

转载自blog.csdn.net/qq_39680564/article/details/108774729
今日推荐