day06——元组,集合

# 元组 练习

# 简单购物车,要求如下:

# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,
# 购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入

# 分析:将商品名,价格,购买个数加入购物列表
# 分析:添加到购物列表 ==> 需要存储商品信息的集合 ==> 列表(商品名,单价,购买个数)
# list ==> list.append(name, price, count)
# dict ==> dict['name'] = ? ... dict['count'] = ?
# 上述两种存储信息均存在隐患
# list ==> 多个商品后,信息混乱
# dict ==> 会出现信息覆盖,只能保存最新的商品
# 结论:需要出现一层集合将一种商品作为整体保存下来,再往集合(购物车)中添加
# 该整体可以用哪些类型
# list
# [name, price, count] => [[name, price, count], [name, price, count]]
# {"name":name, "price":price, "count": count} => [{"name":name, "price":price, "count": count}, {"name":name, "price":price, "count": count}]
# (name, price, count) => [(name, price, count), (name, price, count)]
# dict
# {'1': [name, price, count], '2': [name, price, count]}

# 分析:输入为空或其他非法输入则要求用户重新输入
# 重新 => 循环 => 没有明确出口 => 死循环 => 几个及几层循环
# 最大最外层循环,控制循环输入商品 => 内部结构分析

# 分析:用户输入商品名和购买个数
# 商品名与个数 => 两个输入信息点,如果同时输入,则为 and 的if判断
# => 如果是先后输入,后输入信息一定依赖于前输入信息
# => 如果商品名错误,商品个数没有存在的意义,所以个数依赖于商品名

# 分析:
# 输入商品名逻辑
# 无:直接重新输入 --> continue
# 有:输入个数

# 个数
# 错误:重新输入(从个数还是商品)
# 正确:添加到购物车(购物车逻辑设计已经分析完毕)

# 从新输入个数需要建立内部循环
# 依旧不正确:死循环
# 期间正确:结束自己的本次循环

# 原始商品信息的数据设计

# msg_dic = {
# 'apple': 10,
# 'tesla': 100000,
# 'mac': 3000,
# 'lenovo': 30000,
# 'chicken': 10,
# }
#
# goods_l = []
# while True:
# for key, item in msg_dic.items():
# print('name:{name} price:{price}'.format(price = item,name = key))
# #实现打印商品详细信息
# choice = input('商品>>: ').strip()
# #用户选择购买商品,strip格式化输入
# if not choice or choice not in msg_dic:
# continue
# #商品输入正确,不正确重新输入
# count = input('购买数量>>: ').strip()
# #输入商品的数量
# if not count.isdigit():
# continue
# #判断是不是就是数字
# goods_l.append((choice, msg_dic[choice],count))
# #追加到列表gools_l购物车里
# print(goods_l)

# 字典 练习
# 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
# 将所有大于 66 的值保存至字典的第一个key中,
# 将小于 66 的值保存至第二个key的值中

# dic= {'k1':[],'k2':[]}
# a = [11,22,33,44,55,66,77,88,99]
# for i in a:
# 从a列表中取值
# if i > 66:
# dic['k1'].append(i)
# else:
# dic['k2'].append(i)
#
# print(dic)

# 统计s='hello alex alex say hello sb sb'中每个单词的个数

# 结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}

# s='hello alex alex say hello sb sb'
# l = s.split()#默认用空格
# dic={}
# for i in l:
# if i in dic:
# dic[i]+=1
# else:
# dic[i] = 1
# print(dic)

# s='hello alex alex say hello sb sb'
# dic={}
# words=s.split()
# print(words)
# for word in words: #word='alex'
# dic[word]=s.count(word)
# print(dic) #有重复赋值的问题

#setdefault解决重复赋值
# setdefault的功能
# 1:key存在,则不赋值,key不存在则设置默认值
# 2:key存在,返回的是key对应的已有的值,key不存在,返回的则是要设置的默认值

# d={'a':2222}
# print(d.setdefault('a',1)) #2222 d = {'a': 2222}
# key存在,返回的是key对应的已有的值,key不存在,返回的则是要设置的默认值

# d={}
# print(d.setdefault('a',1)) #1
# s='hello alex alex say hello sb sb'
# dic={}
# words = s.split()
# for word in words:
# dic.setdefault(word,s.count(word))
# print(dic)#解决赋值问题

# s='hello alex alex say hello sb sb'
# dic = {}
# words = s.split()
# words_set = set(words) #利用集合,去掉重复
# print(words_set)
# for word in words:
# dic.setdefault(word,s.count(word))
# print(dic)

# 集合
'''
一.关系运算
  有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
  pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
  linuxs={'wupeiqi','oldboy','gangdan'}

'''
# pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
# linuxs={'wupeiqi','oldboy','gangdan'}
# # 1. 求出即报名python又报名linux课程的学员名字集合
# print(pythons & linuxs)
# # 2. 求出所有报名的学生名字集合
# print(pythons | linuxs)
# # 3. 求出只报名python课程的学员名字
# print(pythons - linuxs)
# # 4. 求出没有同时这两门课程的学员名字集合
# print(pythons ^ linuxs)

# 二.去重
#
# 1. 有列表l=['a','b',1,'a','a'],列表元素均为可hash类型,去重,得到新列表,且新列表无需保持列表原来的顺序
# l=['a','b',1,'a','a']
# print(set(l))
# 2.在上题的基础上,保存列表原来的顺序
# ll=[]
# for i in l:
# if i not in ll:
# ll.append(i)
# print(ll)
#
# l1= []
# s= set()
# for i in l :
# if i not in s: #s中有的就不会进这个循环
# s.add(i)
# l1.append(i)
# print(l1)
# 3.去除文件中重复的行,肯定要保持文件内容的顺序不变
# import os
# with open('db.txt','r',encoding='utf-8') as read_f,
# open('.db.txt.swap','w',encoding='utf-8') as write_f:
# s=set()
# for line in read_f:
# if line not in s:
# s.add(line)
# write_f.write(line)
# os.remove('db.txt')
# os.rename('.db.txt.swap','db.txt')
# 4.有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
#
# l=[
# {'name':'egon','age':18,'sex':'male'},
# {'name':'alex','age':73,'sex':'male'},
# {'name':'egon','age':20,'sex':'female'},
# {'name':'egon','age':18,'sex':'male'},
# {'name':'egon','age':18,'sex':'male'},
# ]
# s = set()
# l1 = []
# for item in l:
# val = (item['name'],item['age'],item['sex'])
# if val not in s:
# s.add(val) #s中有的就不会进这个循环
# l1.append(item)
#
# print(l1)
原文地址:https://www.cnblogs.com/OutOfControl/p/9671108.html