day09

''' 
1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量

2、用列表的insert与pop方法模拟队列


3. 用列表的insert与pop方法模拟堆栈

4、简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  
msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}

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

即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}

6、统计s='hello alex alex say hello sb sb'中每个单词的个数
'''
# 1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量
'''
list1 = ['alex', 49, [1900, 3, 18]]
name, age, birth = list1
year, month, day = birth
print(name, age, birth)
print(year, month, day)
'''

# 2、用列表的insert与pop方法模拟队列
# FIFO: 先进先出
'''
list1 = []
# 根据列表的索引位置插入数据
# 数据开始进入队列中
list1.insert(0, '我是第一个坦克')
list1.insert(1, '我是第二个坦克')
list1.insert(2, '我是第三个坦克')
print(list1)

# 数据开始出队列
list1.pop(0)
print(list1)
list1.pop(0)
print(list1)
list1.pop(0)
print(list1)
'''

# 3. 用列表的insert与pop方法模拟堆栈
'''
# 堆栈: 先进后出
list1 = []
# 根据列表的索引位置插入数据
# 数据开始进入队列中
list1.insert(0, '我是第一个坦克')
list1.insert(1, '我是第二个坦克')
list1.insert(2, '我是第三个坦克')
print(list1)

list1.pop()  # 默认取出列表中最后一个数据
print(list1)
list1.pop()  # 默认取出列表中最后一个数据
print(list1)
list1.pop()  # 默认取出列表中最后一个数据
print(list1)
'''

# 4、简单购物车,要求如下:
# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,
# 如果输入为空或其他非法输入则要求用户重新输入  
'''
msg_dic = {
    'apple': 10,
    'tesla': 100000,
    'mac': 3000,
    'lenovo': 30000,
    'chicken': 10,
}
while True:
    # 实现无限循环打印商品信息,让用户选择商品
    for key, value in msg_dic.items():
        # 格式化输出方式一:
        # print('商品名称:[%s]  单位: [%s]元' % (key, value))

        # 格式化输出方式二:
        # print('商品名称:[{}]  单位: [{}]元'.format(key, value))

        # 格式化输出方式三:  f'{key}{value}' ---> 等同于 '{}{}'.format(key, value)
        # 该语法是python3.5以后才出来
        print(f'商品名称:[{key}]  单位: [{value}]元')

    # 让用户输入商品名称
    shop_name = input('请输入需要购买的商品名称: ').strip()

    # 校验用户输入的商品是否存在
    if shop_name not in msg_dic:
        print('当前商品不存在')
        continue

    # 让用户输入购买的商品个数
    count = input('请输入购买的商品个数:').strip()
    if count.isdigit():
        count = int(count)

        shop_price = msg_dic.get(shop_name)

        cost = shop_price * count

        # 将商品的名称、单价、个数、总价 存放在一个元组中
        shop_tuple = (shop_name, shop_price, count, cost)
        print(shop_tuple)

        # 让用户选择是否继续购买
        break

    else:
        print('个数必须是数字类型!')
'''

# 5、有如下值集合 [11,22,33,44,55,66,77,88,99,90...],
# 将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
# 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
# {'k1': [], 'k2': []}
'''
list1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
dict1 = {'k1': [], 'k2': []}
# 循环遍历获取list1中的每一个值,然后一一比较
for line in list1:
    if line > 66:
        dict1['k1'].append(line)
    elif line < 66:
        dict1['k2'].append(line)
print(dict1)
'''

# 6、统计s='hello alex alex say hello sb sb'中每个单词的个数
'''
s = 'hello alex alex say hello sb sb'
new_list = s.split(' ')
print(new_list)
dic = {}
for line in new_list:
    # 如果当前的line不存在dic字典中,代表该单词是第一个出现,也就是个数为1
    if line not in dic:
        # 若单词出现第一次,则将该单词当做字典的key,1作为字典的value
        # 初始化单词的个数
        dic[line] = 1

    else:
        # 若是重复出现的单词,则个数 += 1
        dic[line] += 1

print(dic)
'''
原文地址:https://www.cnblogs.com/2722127842qq-123/p/13449100.html