# 2021年12月01日,第二周13~14,第三周1~3
# !/usr/bin/env python
# -*- coding: utf-8 -*-
def day5_case1():
# 三级菜单实列,程序可优化
data = {
"北京": {
"昌平": {
"沙河": ["小董家", "大董家"],
"天通苑": ["小卖部", "大卖部"],
},
"朝阳": {
"望京": ["奔驰", "华为"],
"国贸": ["惠普", "飞信"],
},
},
"山东": {
"德州": {},
"青岛": {},
"济南": {},
},
"广东": {
"东莞": {},
"佛山": {},
"广州": {},
},
}
while True:
for i in data:
print(i)
choice = input("选择进入:")
if choice in data:
while True:
for i1 in data[choice]:
print("\t", i1)
choice1 = input("选择进入:")
if choice1 == "b": break
if choice1 in data[choice]:
while True:
for i2 in data[choice][choice1]:
print("\t\t", i2)
choice2 = input("选择进入:")
if choice2 == "b": break
if choice2 in data[choice][choice1]:
for i3 in data[choice][choice1][choice2]:
print("\t\t\t", i3)
choice3 = input("输入b返回,输入q退出:")
if choice3 == "b": pass
if choice3 == "q": exit()
def day5_case2():
# 集合
# 集合是一个无序的,不重复的数据组合,它的主要作用如下:
# 去重,把一个列表变成集合,就自动去重了
# 关系测试,测试两组数据之前的交集、差集、并集等关系
list_1 = [1, 2, 3, 4, 12, 4, 2, 5, 2]
print("list_1", list_1, type(list_1))
list_1 = set(list_1) # 把列表变成集合
print("list_1", list_1, type(list_1)) # 集合具备去重作用,而且是无序的
list_2 = set([1, 2, 3, 4, 5, 13, 2, 4, 2]) # 创建一个集合
print("list_2", list_2)
print("交集", list_1.intersection(list_2)) # 取出交集
print("交集", list_1 & list_2) # 取出交集
print("判断是否无交集", list_1.isdisjoint(list_2)) # 判断是否无交集
print("并集/合集", list_1.union(list_2)) # 求并集/合集
print("并集/合集", list_1 | list_2) # 求并集/合集
print("list_1与list_2差集", list_1.difference(list_2)) # 求list_1与list_2差集
print("list_2与list_1差集", list_2.difference(list_1)) # 求list_2与list_1差集
print("list_2与list_1差集", list_2 - list_1) # 求list_2与list_1差集
print("对称差集", list_1.symmetric_difference(list_2)) # 对称差集:返回两个集合组成的新集合,但会移除两个集合的重复元素
print("对称差集", list_1 ^ list_2) # 对称差集:返回两个集合组成的新集合,但会移除两个集合的重复元素
print("list_1是否为list_2子集", list_1.issubset(list_2)) # 求子集
list_3 = set([1, 2, 3])
print(list_3.issubset(list_2)) # 求list_3是否list_2的子集
print("list_3是否为list_2子集", list_2.issuperset(list_3)) # 求list_2是否包含list_2,即list_3是否为list_2的子集
# 集合的基本操作
gather = set("1")
gather.add("2") # 添加一个值
gather.update("2", "1", "3") # 添加多个值
print("集合gather", gather)
gather.remove("2")
print("集合gather", gather)
gather = set(["1", "3"])
gather1 = set(["1", "2", "3", "4"])
print("集合gather", gather)
print("集合gather1", gather1)
print("不可用in判断一个集合是否在另一个集合里", gather in gather1) # 不可用in判断gather是否在gather1里
print("判断gather是否在gather1里", gather <= gather1) # 判断gather是否在gather1里
gather1.pop() # 随机出栈一个值,随机是因为字典是无序的
print("集合gather1", gather1)
def day5_case3():
pass
if __name__ == '__main__':
day5_case2()
Python-day5:集合、三级菜单实列
最新推荐文章于 2024-07-31 14:30:10 发布