1、字典是一个容器类,可以用来存储数据
字典是以键值对(key:value)的模式存储数据,key 值必须是不可变的,一般使用字符串作为字典中的key,也可使用数字等不可变类型的值。
key 在字典中是唯一的,若有多个相同key值,只保留最后一个
字典中存储的数据是无序的
dict_one = {
'name'
:
'Tjx_Miracle'
,
'age'
:
22
,
'sex'
:
'man'
}
2、根据key取出字典中的值
name = dict_one[
'name'
]
若key不存在会报错,这时可使用 get(key,default)函数获取字典中的值,若key不存在,则赋给变量default
name = dict_one.get(
'sss'
,
'数据不存在'
)
3、修改字典中key对应的value 或 向字典中添加数据
dict_one[
'car'
] =
'BMW'
key不存在时,则向字典中添加数据,key存在则修改其对应得value值
4、根据key删除字典中的数据
del
dict_one[
'car'
]
pop(key) 函数,key必要参数,要移出的数据对应的key
sex = dict_one.pop(
'sex'
)
popitem()函数,随机移出字典中的一个键值对,并且将键值对放在元组中
t = dict_one.popitem()
5、删除字典中所有的key
dict_one.clear()
6、获取字典中所有的key
keys = dict_one.keys()
keys类型 => <class 'dict_keys'>,可用泛型遍历取出所有key值
7、
获取字典中所有的value
values = dict_one.values()
values类型 => <class 'dict_values'>,可用泛型遍历取出所有key值
8、
获取字典中所有的键值对
items = dict_one.items()
items类型 => <class 'dict_items'>,可用泛型遍历取出所有key值
# 简易产品管理系统
class Phone_Sale(object): def __init__(self): self.phone_list = [] # 判断选项是否存在 def judge_exist(self): while True: chose = int(input('该选项不存在,请重新选择:')) return chose # 展示库存商品详细信息 def show_info(self): for index in range(0,len(self.phone_list)): print('>>>>>>>> 产品序号:%s 产品名称:%s 产品价格:%s 产品库存:%s <<<<<<<<' % (index,self.phone_list[index]['phone_name'],self.phone_list[index]['phone_price'], self.phone_list[index]['phone_repertory'])) # 1、查看所有手机品牌 def query_brand(self): print('查看所有手机品牌:') for x in range(0,len(self.phone_list)): print('>>>>>>>> 产品序号:%s 产品名称:%s <<<<<<<<' % (x,self.phone_list[x]['phone_name'])) print('=======================================================================') print('>>>>>>>>>>>>>>>>>>>>>>>> 1.选择产品序号查看详情 <<<<<<<<<<<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>>>>>>>>>>> 2.返回 <<<<<<<<<<<<<<<<<<<<<<<<') print('=======================================================================') chose = int(input('请选择你的操作:')) if chose not in range(1, 3): chose = self.judge_exist() print('=======================================================================') if chose == 1: # ru if len(self.phone_list): index = int(input('请输入产品序号:')) if index not in range(0, len(self.phone_list)): index = self.judge_exist() print('>>>>>>>> 产品名称:%s 产品价格:%s 产品库存:%s<<<<<<<<' % (self.phone_list[index]['phone_name'],self.phone_list[index]['phone_price'],self.phone_list[index]['phone_repertory'])) print('=======================================================================') print('>>>>>>>>>>>>>>>>>>>>>>>> 1.购买 <<<<<<<<<<<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>>>>>>>>>>> 2.返回 <<<<<<<<<<<<<<<<<<<<<<<<') print('=======================================================================') select = int(input('请选择你的操作:')) if select not in range(1, 3): select = self.judge_exist() print('=======================================================================') if select == 1: self.phone_list[index]['phone_repertory'] = int(self.phone_list[index]['phone_repertory']) - 1 while self.phone_list[index]['phone_repertory'] == 0: del self.phone_list[index] elif select == 2: pass else: print('没有产品信息,请添加产品信息.') # 2、更改产品库存信息 def updata_info(self): print('更改产品库存信息:') print('>>>>>>>>>>>>>>> 1、添加新产品 <<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>> 2、修改原有产品 <<<<<<<<<<<<<') print('=======================================================================') chose = int(input('请选择你的操作:')) if chose not in range(1, 3): chose = self.judge_exist() print('=======================================================================') if chose == 1: phone_name = input('请输入产品名称:') phone_price = float(input('请输入产品价格:')) phone_repertory = int(input('请输入产品库存:')) phone_dict = {'phone_name':phone_name,'phone_price':phone_price,'phone_repertory':phone_repertory} self.phone_list.append(phone_dict) elif chose == 2: # # 调用函数,展示商品详细信息 self.show_info() print('=======================================================================') index = int(input('请选择商品序号')) if index not in range(0, len(self.phone_list)): index = self.judge_exist() print('=======================================================================') phone_info = self.phone_list[index] phone_info['phone_name'] = input('请输入修改后的产品名称(原:%s):' % phone_info[0]) phone_info['phone_price'] = float(input('请输入修改后的产品价格(原:%s):' % phone_info[1])) phone_info['phone_repertory'] = int(input('请输入修改后的产品库存(原:%s):'% phone_info[2])) print('=======================================================================') # 3.移除产品库存信息 def remove_info(self): print('移除产品库存信息:') print('>>>>>>>>>>>>>>> 1、查看所有产品,根据序号移除 <<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>> 2、移除所有产品 <<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>> 3、返回 <<<<<<<<<<<<<<<') print('=======================================================================') chose = int(input('请选择你的操作:')) # 调用函数,判断选项是否存在 if chose not in range(1, 4): chose = self.judge_exist() print('=======================================================================') if chose == 1: # 调用函数,展示商品详细信息 self.show_info() print('=======================================================================') index = int(input('请选择产品序号')) if index not in range(0, len(self.phone_list)): index = self.judge_exist() flag = input('>>>>>>>>>>>>>>>>>> 你确定要删除此产品信息?(Y/N) <<<<<<<<<<<<<<<<<<') if flag == 'Y': del self.phone_list[index] elif flag == 'N': pass elif chose == 2: flag = input('>>>>>>>>>>>>>>>>> 你确定要删除所有产品信息?(Y/N) <<<<<<<<<<<<<<<<<') if flag == 'Y': self.phone_list.clear() elif flag == 'N': pass elif chose == 3: pass def start(self): while True: print(' ============================ 手机销售系统 ============================ ') print(' ============================ 出品人:Tjx_Miracle ===================== ') print('>>>>>>>>>>>>>>>>>> 1.查看所有手机品牌 <<<<<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>>>>> 2.更改产品库存信息 <<<<<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>>>>> 3.移除产品库存信息 <<<<<<<<<<<<<<<<<<') print('>>>>>>>>>>>>>>>>>> 4.退出程序 <<<<<<<<<<<<<<<<<<') print('======================================================================= ') chose = int(input('请选择你的操作:')) # 调用函数,判断选项是否存在 if chose not in range(1, 5): chose = self.judge_exist() print('========================================================================') if chose == 1: # 1、查看所有手机品牌 self.query_brand() elif chose == 2: # 2、更改产品库存信息 self.updata_info() elif chose == 3: # 3.移除产品库存信息 self.remove_info() elif chose == 4: break phone = Phone_Sale() phone.start()