python『学习之路03』集合系列, 附多级菜单demo

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/11/18 5:50
# @Author : mixiu26

# 集合:Set ---- > 唯一, 无序
list1 = [1,4,5,6,7,2,4,6]
# 列表去重: ---- >> 直接将列表转成集合即可: set(list1)
list1 = set(list1)
# 打印list1及类型:
print(list1,type(list1)) # {1, 2, 4, 5, 6, 7} <class 'set'> 集合无序,元素不重复

# 集合交集: intersection() ----- >> 求交集
list2 = set([2,3,4,5,6])
print(list1,list2) # {1, 2, 4, 5, 6, 7} {2, 3, 4, 5, 6}
print(list1.intersection(list2)) # {2, 4, 5, 6}

# 集合并集: 合并去重:union()
print(list1.union(list2)) # {1, 2, 3, 4, 5, 6, 7}

# 差集: difference()
print(list1.difference(list2)) # {1, 7} 取出所有1中有,2中没有的元素, 取出所有调用者中有,比较者中没有的元素
print(list2.difference(list1)) # {3} 取出所有2中有,一种没有的元素

# 子集: issubset ----- >> 是你的一部分
print(list2.issubset(list1)) # False list2 是 list1 的子集吗?
list3 = set([2,4,5,6,7])
print(list3.issubset(list1)) # True list3 是 list1的子集:

# 父集:issuperset:
print(list1.issuperset(list3)) # True list1 是3的父集
print(list1.issuperset(list2)) # False list1不是2的父集

# 反向差集: 取出彼此间互相都没有对方的元素: 就是只有对方一人有的元素,全部取出来
print(list1.symmetric_difference(list2)) # {1, 3, 7} // 去除二人交集的部分 ---- >> 补集

list4 = set([11,22,33,44,55])
# 判断两集合中是否有交集:
print(list1.isdisjoint(list2)) # False 二者间有交集变返回false
print(list1.isdisjoint(list4)) # True 二者间如果没有交集就返回true

# 集合表示符: & 1 ^ - 交并补差
print(list1,list2)
print(list1 & list2) # {2, 4, 5, 6}
print(list1 | list2) # {1, 2, 3, 4, 5, 6, 7}
print(list1 ^ list2) # {1, 3, 7}
print(list1 - list2) # {1, 7}

# 集合 ---- >> 增删改查:
list1.add(111)
print(list1) # {1, 2, 4, 5, 6, 7, 111}

# 批量添加元素:
# list1.update(11,22,33) 需要用集合的格式去添加,不能用添加单个元素的方式来批量添加:
list1.update([11,22,33])
print(list1) # {1, 2, 33, 4, 5, 6, 7, 11, 111, 22} --- >> 提现无序性了吧

# 删除元素:
list1.remove(11)
print(list1) # {1, 2, 33, 4, 5, 6, 7, 111, 22}

# 打印集合长度,元素个数:
print(len(list1)) # 9

# 判断一个元素是否在一个集合,列表,字典中用法: x in a ---- >a 可以是list ,集合 字符串
print(11 in list1) # False
print(111 in list1) # True

# pop() --- >>随机删除一个元素并返回:
# print(list1.pop()) # 1

# discard ---- >> 删除指定元素:
print(list1.discard(1),list1) # None {2, 33, 4, 5, 6, 7, 111, 22} 必须携带元素删除, 只删除元素,删除后不返回已删除元素,remove删除不存在元素会报错,discard不会
print(list1.discard(9876),list1) # None {2, 33, 4, 5, 6, 7, 111, 22}

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/11/18 10:55
# @Author : mixiu26

data = {
'北京':{
'昌平':{
'沙河':['老男孩',"test"],
'天通苑':["链家地产","我爱我家"]
},
'朝阳':{
"望京":["奔驰","陌陌"],
"国贸":{"CICC","HP"},
"东直门":{"Advent","飞信"},
},
'海定':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车战':{
'携程':{}
}
},
'浦东':{},
},
'山东':{
"德州":{},
"青岛":{},
"济南":{}
}
}

exit_flag = False

while not exit_flag:
for i in data:
print(i)
choice = input("请选择一级菜单城市: ")
# 判断选择城市是否在二级菜单列表范围:
if choice in data:
while not exit_flag: # 如果你输入的城市不再我的列表范围,我可以循环展示城市列表
for i2 in data[choice]:
print(" " + i2)
# 继续等待用户输入:
choice2 = input("请选择城市二级区域列表菜单 >>>: ")
if choice2 in data[choice]:
while not exit_flag:
for i3 in data[choice][choice2]:
print(" " + i3)
choice3 = input("请选择三级街道 >>>: ")
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print(" " + i4)
# 在打印完列表时,我们并不希望它马上退出程序,因为我希望它可以做个选择
# 如果我输入的是q我就退出程序,我输入的是d,就返回上级菜单
choice4 = input("终极菜单层, 是否退出程序 >>>:")
if choice4 == "b":
pass # 直接返回上级菜单
elif choice4 == "q":
# 改变flag的值,退出程序
exit_flag = True
else: # 如果不加这段判断, 会出现一个问题,因为我的choice中走的是pass, 到这层时,输入任何字符他否会返回上级菜单,所以在else中加入校验:
print("当前目录已是最后一级,您可进行选择是否退出程序 >>>: b/q")
while not exit_flag :
choice5 = input("请输入选择 >>>: ")
if choice5 == "b":
break
elif choice5 == "q":
# 改变flag的值,退出程序
exit_flag = True
if choice3 == "b":
break # 直接返回上级菜单
elif choice3 == "q":
exit_flag = True
if choice2 == "b":
break # 直接返回上级菜单
elif choice2 == "q":
# 改变flag的值,退出程序
exit_flag = True

 



原文地址:https://www.cnblogs.com/mixiu26/p/7856272.html