python的数据容器(2)字典、集合与遍历

目录

字典(dict)

字典的基本操作

集合(set)

集合的创建

集合的基本操作

遍历


字典(dict)

  • 字典是一种无序的集合,用于存储键值对(Key-Value pairs)。
  • 字典中的每个元素都是一个键值对,键必须是唯一的,而值则可以是任何数据类型。
  • 字典是可变的,但仅限于添加、删除或修改键值对,键本身是不可变的。
  • 使用花括号 {} 来定义字典。

示例:

dict_a = {1: 2, 2: 3, True: False, "a": "b", (1, 2): (3, 4)}
print(dict_a)  # 后面出现的值会将前面的值覆盖

list_a = ["a", "b", "c"]
list_b = [88, 99, 100]
print(list_b[list_a.index("a")])

dict_b = {"a": 88, "b": 99, "c": 100}
print(dict_b["a"])

输出结果:

E:\shujia\python.exe F:\code\day03.py 
{1: False, 2: 3, 'a': 'b', (1, 2): (3, 4)}
88
88

Process finished with exit code 0

字典的基本操作

pop:弹出指定的key

示例:

dict_a = {1: 2, 2: 3, True: False, "a": "b", (1, 2): (3, 4)}
# pop方法 弹出指定的key
print(dict_a.pop(1))
print(dict_a)

输出结果:

E:\shujia\python.exe F:\code\day03.py 
False
{2: 3, 'a': 'b', (1, 2): (3, 4)}

Process finished with exit code 0

get:获取指定的key的value,比直接通过key取值更安全。

示例:

dict_a = {1: 2, 2: 3, True: False, "a": "b", (1, 2): (3, 4)}
print(dict_a.get("a"))
print(dict_a.get("hello"))  # 不会报错

输出结果:

E:\shujia\python.exe F:\code\day03.py 
b
None

Process finished with exit code 0

 ietm:将键值对以二元组的形式返回

示例:
dict_a = {1: 2, 2: 3, True: False, "a": "b", (1, 2): (3, 4)}
print(dict_a.items())

print(dict_a.keys())  # 取键
print(dict_a.values())  # 取值

输出结果:

E:\shujia\python.exe F:\code\day03.py 
dict_items([(1, False), (2, 3), ('a', 'b'), ((1, 2), (3, 4))])
dict_keys([1, 2, 'a', (1, 2)])
dict_values([False, 3, 'b', (3, 4)])

Process finished with exit code 0

 popitem:弹出一个键值对

示例:


dict_a = {1: 2, 2: 3, True: False, "a": "b", (1, 2): (3, 4)}
print(dict_a.popitem())
print(dict_a)

输出结果:

E:\shujia\python.exe F:\code\day03.py 
((1, 2), (3, 4))
{1: False, 2: 3, 'a': 'b'}

Process finished with exit code 0

 字典增加值

示例:

dict_a = {1: 2, 2: 3, True: False, "a": "b", (1, 2): (3, 4)}
dict_a['bb'] = 'aa'
print(dict_a)

输出结果:

E:\shujia\python.exe F:\code\day03.py 
{1: False, 2: 3, 'a': 'b', 'bb': 'aa'}

Process finished with exit code 0

集合(set)

  • 集合是一个无序的、不包含重复元素的集合。
  • 集合主要用于进行数学上的集合运算,如并集、交集、差集等。
  • 集合是可变的,即可以添加或删除元素,但不能修改元素(因为集合中的元素是无序且不重复的)。
  • 使用大括号 {} 来定义集合(但集合中的元素不是键值对,与字典不同),或者使用 set() 函数

集合的创建

使用大括号 {} 创建集合,示例:

set_a = {1, 2, 3, 4, 5, 5, 6, 6, 7, 1}
set_d = {4, 5, 6, 7, 8, 9}
set_b = set("abcd")

print(set_a)
print(set_b)

输出结果:

E:\shujia\python.exe F:\code\day004.py 
{1, 2, 3, 4, 5, 6, 7}
{'b', 'd', 'c', 'a'}

Process finished with exit code 0

集合中的元素可以是任何不可变类型(如整数、浮点数、字符串、元组),但不能是列表或字典等可变类型。

集合的基本操作

添加元素:使用 .add()方法添加单个元素,使用 .update方法添加多个元素(可以是集合、列表、元组等)。


set_a = {1, 2, 3, 4, 5, 5, 6, 6, 7, 1}
set_a.add(10)
print(set_a)
set_a.update([8,9])
print(set_a)

 输出结果:

E:\shujia\python.exe F:\code\day004.py 
{1, 2, 3, 4, 5, 6, 7, 10}
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Process finished with exit code 0

移除元素:使用 .remove() 方法移除指定元素(如果该元素不存在,则抛出 KeyError),使用 .discard() 方法移除指定元素(如果该元素不存在,则不执行任何操作),使用 .pop() 方法移除并返回集合中的任意一个元素(如果集合为空,则抛出 KeyError)。

示例:

set_a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
# remove 删除不存在的会报错 discard 删除不存在的不会报错
set_a.remove(10)
print(set_a)
set_a.discard(10)
print(set_a)
# pop 随机弹出一个元素
print(set_a.pop())
print(set_a)

输出结果:

E:\shujia\python.exe F:\code\day004.py 
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
1
{2, 3, 4, 5, 6, 7, 8, 9}

Process finished with exit code 0

 

集合运算:支持并集、交集、差集运算。

 示例:

# intersection 交集
print(set_a.intersection(set_d))

# union 并集
print(set_a.union(set_d))

# difference差集
print(set_a.difference(set_d))
print(set_d.difference(set_a))

输出结果:

E:\shujia\python.exe F:\code\day004.py 
{4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3}
{8, 9}

Process finished with exit code 0

 

遍历

遍历是一种用于访问数据结构(如数组、链表、树、图等)中所有元素的过程,且每个元素仅被访问一次。遍历的具体实现方式取决于数据结构的类型。

遍历列表、元组、集合

直接遍历列表、元组与集合,示例:

num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num_tuple = (10, 20, 30, 40, 50, 60, 70, 80, 90)
num_set = {5, 15, 25, 35, 45, 55, 65, 75, 85, 95}
# 直接遍历
for i in num_list:
    print(i, end=" ")

for i in num_tuple:
    print(i, end=" ")

for i in num_set:
    print(i, end=" ")

输出结果:

E:\shujia\python.exe F:\code\day03.py 
1 2 3 4 5 6 7 8 9 
10 20 30 40 50 60 70 80 90 
65 35 5 75 45 15 85 55 25 95 

Process finished with exit code 0

 通过索引遍历列表与元组,示例:

num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num_tuple = (10, 20, 30, 40, 50, 60, 70, 80, 90)
# 用索引遍历
for i in range(len(num_list)):
    print(num_list[i])

for i in range(len(num_tuple)):
    print(num_tuple[i])

输出结果:

E:\shujia\python.exe F:\code\day03.py 
1 2 3 4 5 6 7 8 9 
10 20 30 40 50 60 70 80 90 


Process finished with exit code 0

 同时遍历列表与元组,示例:

num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num_tuple = (10, 20, 30, 40, 50, 60, 70, 80, 90)
# 同时遍历列表和元组
for a, b in zip(num_list, num_tuple):
    print(a, b)

输出结果:

E:\shujia\python.exe F:\code\day03.py 
1 10 2 20 3 30 4 40 5 50 6 60 7 70 8 80 9 90 
Process finished with exit code 0

 字典的遍历

示例:

num_dict = {"a": "b", "c": "d", 1: 2, 3: 4, (5, 6): [7, 8]}
# 字典的遍历
for k in num_dict.keys():
    print(k, num_dict.get(k),end = " ")
print("\n")
# 通过items返回的二元组去接收返回值  然后用两个变量去接收二元组内的值
for k, v in num_dict.items():
    print(k, v)

 输出结果:

E:\shujia\python.exe F:\code\day03.py 
a b c d 1 2 3 4 (5, 6) [7, 8] 

a b c d 1 2 3 4 (5, 6) [7, 8] 
Process finished with exit code 0
  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值