python -- 结构数据类型(列表、元组、集合、字典)

一、列表

列表表示一组有序的元素,这些元素可以是数字、字符串,也可以是另一个列表。

# ----------------------------------------
# 列表(list):一组有序的元素
# ----------------------------------------
# 定义一个空的列表
s = []
# 定义一个全是数字的成绩列表
score = [90, 92, 88, 79, 95, 82]

# 可以通过索引值来访问列表中的元素,从左到右,起始值为0,最后一个元素为n-1
first_one = score[0]
# 也可以从右到左通过一个负数索引值进行访问,倒数第一个元素为-1,最后一个元素为-n
last_one = score[-1]

# 修改元素的值
score[-1] = 80
print(score)

# 列表中的元素可以是不同的数据类型
student_info = ['1001', '张晓晓', '女', 12, [86, 88, 92]]
# 使用切片打印学生基本信息
print("学生基本信息:", student_info[1:4])

# + 将运算符两侧的列表组合在一起
grade = ['七年级']
student_info = grade + student_info
print(student_info)

# 同理也可以用+=
student_info += ['广东省', '广州市']
print(student_info)

# append() 在列表尾部追加新的元素(一次只能追加一个元素)
student_info.append('番禺区')
print(student_info)

# extend() 在列表尾部追加另一个列表(追加的不是列表,而是列表中的元素)
address = ['南村镇', '东环街']
student_info.extend(address)
print(student_info)

# insert(x, y) 向列表中的x位置插入数据y
student_info.insert(-1, "同乐路") #由于东环街是-1,对应同乐路,则不是添加到末尾
student_info.insert(len(student_info), "同乐路") #把同乐路加入到末尾
print(student_info)

# * 将列表中的元素重复n次,并且拼接成一个列表
scores = [90, 100] * 5
print(scores)

# 使用in和not in判断某个元素是否存在于列表中
print("100 in scores:", 100 in scores)
print("100 not in scores:", 100 not in scores)

# len() 返回列表的长度
score = [90, 92, 88, 79, 95, 90, 82]
print("score列表中有%d个元素" % len(score))

# max() 返回列表元素中的最大值(元素的数据类型必须一致,否则会出错)
print("最高分是:", max(score))
# min() 返回列表元素中的最小值
print("最低分为:", min(score))
# sum() 返回列表中所有元素的和
print("总分:", sum(score))

# count(x) 统计列表中相同元素值x出现的次数
print("90分有%d个" % score.count(90))

# sort() 将列表中的元素进行排序,默认为升序(需要是统一的单一数据类型)
score.sort()
print("升序排列:", score)
score.sort(reverse=True)
print("降序排列:", score)

# index(x) 获得指定元素x第一次在列表中的位置(索引值)
index = score.index(90)
print("90分的排名为:", index + 1)

# pop(index) 删除列表中索引值为index的元素,默认删除最后一个元素
score.pop()
print("删除一个最低分:", score)

# reverse() 将列表中的元素颠倒排列
score.reverse()
print("将列表中的元素进行颠倒:", score)

# remove(value) 将列表中元素值为value的项删除,一次只删除一个元素
score.remove(90)
print("删除90分:", score)

# clear() 清空列表
score.clear()
print(score)

score = [90, 92, 88, 79, 95, 90, 82]
# del 删除指定位置的元素,并且可以使用切片的方式进行删除
del score[2]
print(score)
del score[0:3]
print(score)
del score
print(score)


二、元组
元组也是一组有序的元素,一旦定义,就不能更改了。

# ----------------------------------------
# 元组(tuple):一组有序的元素。定义后元素不可修改
# ----------------------------------------
# 创建只有一个元素元组,必须在元素之后加上逗号
# t = ()
# one = (100)
only_one = (100,)

# + 对元组进行拼接,生成一个新的元组
info1 = '1001', '张晓晓'
info2 = ('女', '初一')
print("info1+info2:", info1 + info2)

# * 重复多次
print("info1*3:", info1 * 3)

# 使用in和not in判断某个元素是否存在于元组中
print("张晓晓 in info1:", '张晓晓' in info1)
print("张晓晓 not in info1:", '张晓晓' not in info1)

student_info = ('1001', '张晓晓', '女', '初一', 12, '广州')
# len() 返回元组的长度(元组中元素的个数)
print("元组的长度:", len(student_info))
# 同样可以通过索引和切片的方式访问元组
print(student_info[1:])

tuple1 = (88, 93, 76, 90)
# max() 返回元组中元素的最大值(元组中的数据类型必须一致)
print("最大值为:", max(tuple1))
# min() 返回元组中元素的最小值(数据类型必须一致)
print("最小值为:", min(tuple1))

# 将列表转换为元组
list2 = [100, 200, 300, 200, 400]
tuple2 = tuple(list2)
print("将列表转换为元组:", tuple2)

# sum() 返回元组中所有元素的和
print("总计:", sum(tuple2))

# count() 统计指定元素在元组中出现的次数
print("200出现的次数:", tuple2.count(200))

# index() 返回指定元素第一次在元组中出现的索引值
print("200第一次出现的位置:", tuple2.index(200))

# 将元组转换为列表
list3 = list(student_info)
print("将元组转换为列表:", list3)

# 元组中的元素值不允许删除,只能使用del语句删除整个元组
del student_info

三、集合
集合是一个无序的不重复的元素序列

# ----------------------------------------
# 集合(set):一个无序的不重复元素序列
# ----------------------------------------
# 创建一个空集合,必须使用set(),不能使用{}
set1 = set()

# 集合会自动去掉重复数据
cities = {'北京', '上海', '广州', '深圳', '海南', '广州'}
print(cities)

# 可以先创建一个列表,然后使用set函数将列表转化为集合
list2 = [100, 200, 300, 400, 300]
set2 = set(list2)
print(set2)

# 通过一个字符串来创建字符集合
set3 = set("Hello")
print(set3)

# 使用in和not in判断某个元素是否存在于集合中
print("200 in set2:", 200 in set2)
print("00 not in set2:", 200 not in set2)

a = {100, 200, 300}
b = {300, 400}
# a-b:差集(返回一个新的集合,包含a中有但b中没有的元素)
print("a-b:", a - b)
print(a.difference(b))

# a|b:并集(返回一个新的集合,包含a和b中的每一个元素)
print("a|b:", a | b)
print(a.union(b))

# a&b: 交集(返回一个新的集合,包含a和b中的公共元素)
print("a&b:", a & b)
print(a.intersection(b))

# a^b: 对称差集(返回一个新的集合,包含没有同时出现在a和b中的元素)
print("a^b:", a ^ b)
print(a.symmetric_difference(b))

# add() 往集合中添加一个元素
a.add(400)
print("add:", a)
# update() 更新集合中的元素。可以往集合中添加单个元素、列表、元组等
a.update([500, 600])
print("update:", a)

# a<=b:判断a是否是b的子集(检查a中的每一个元素是否都在b中)
print("a是b的子集:", a <= b)
# a>=b: 判断a是否是b的父集(检查b中的每一个元素是否都在a中)
print("a是b的父集:", a >= b)

# remove(x) 将元素x从集合中移除,如果元素不存在会报错
a.remove(600)
print("remove result:", a)

# discard(x) 也是将元素x从集合中移除,当元素不存在时不会报错
a.discard(500)
print("discard:", a)

# pop() 随机删除集合中的一个元素
a.pop()
print("pop:", a)

# len() 计算集合中元素的个数
print("集合中的元素个数:", len(a))

# clear() 清空集合
a.clear()


四、字典

字典是一种可变容器模型,可以存储任意类型的对象,由键-值对组成。
# ----------------------------------------------------------
# 字典(dict):一种可变容器模型,可以存储任意类型的对象。由键-值对组成
# ----------------------------------------------------------
# 键必须是唯一且不可改变的
dic = {1: '一季度', '2': '二季度'}
# 不能通过索引访问,只能通过键进行访问
print(dic['2'])

dic = {1: '一季度', '2': '二季度', '2': '三季度'}
print(dic)

dic['2'] = '二季度' # 修改元素的值
dic['3'] = '三季度' # 增加新的键值对
print(dic)

del dic['3'] # 删除字典中的元素
print(dic)

# len() 计算字典元素的个数,即键值的总数
print("字典长度为:", len(dic))

# str() 将字典的元素转化为可打印的字符串形式
print(str(dic))

# copy() 复制字典(浅拷贝)
dic1 = dic.copy()
print("dic1:", dic1)

# clear() 清除字典中的所有元素
dic.clear()
print("dic:", dic)

dic3 = {'一季度': 10000, '二季度': 12000, '三季度': 18000}
# get(key[, value]) 返回指定键的值。如果指定的键不存在,返回value
print("一季度的销量为:", dic3.get('一季度'))
print("四季度的销量为:", dic3.get('四季度'))
print("四季度的销量为:", dic3.get('四季度', '未统计'))

# setdefault(k[, v]) 如果k存在,就返回其值;否则返回v,并将新的元素添加到字典中
print(dic3.setdefault('一季度'))
print(dic3.setdefault('四季度', 17000))

# 使用in和not in检测键(key)是否存在于字典中
print("一季度存在于字典中:", '一季度' in dic3)
print("四季度不存在于字典中:", '四季度' not in dic3)

# items() 使用字典中的元素创建一个由元组对象组成的列表(一个元组对应一个键-值对)
print(dic3.items())

# keys() 使用字典中的键创建一个列表
print(dic3.keys())

# values() 使用字典中的值创建一个列表
print(dic3.values())

# dic1.update(dic2) 将字典dic2的键/值对更新到dic1中
dic4 = {'一月份': 3500, '二月份': 3800}
dic3.update(dic4)
print(dic3)

# pop() 删除字典中指定key对应的值,并且返回被删除的值
print(dic3.pop('一月份'))

# popitem() 删除字典中的最后一元素,并返回这个元素的键值
print(dic3.popitem())

猜你喜欢

转载自www.cnblogs.com/Teachertao/p/11210563.html