列表、元组、字典-练习👈

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

li = ['alex', 49, [1900, 3, 18]]
# 解压赋值:不用取值操作
name, age, year_month_date = li
year, month, date = year_month_date
print(name, age, year, month, date)  # alex 49 1900 3 18

2、用列表的insert与pop方法模拟队列(面试题)

# 列队:FIFO 先进先出(提示:面试会问你什么是FIFO。 FIFO 全称 first in first out)
li = []
# 入队
s = '入队'
print(s.center(50, '-'))
for index in range(5):
    li.insert(index, index)
    print(li)

# 出队
s = '出队'
print(s.center(50, '-'))
for i in range(5):
    li.pop(0)
    print(li)
'''
------------------------入队------------------------
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
------------------------出队------------------------
[1, 2, 3, 4]
[2, 3, 4]
[3, 4]
[4]
[]
'''

3、用列表的insert与pop方法模拟堆栈(面试题)

# 堆栈:FILO 先进后出(提示:面试会问你什么是FILO。 FILO 全称 first in last out)
li = []
# 入栈
s = '入栈'
print(s.center(50, '-'))
for index in range(5):
    li.insert(index, index)
    print(li)
# 出栈
s = '出栈'
print(s.center(50, '-'))
for i in range(5):
    li.pop()
    print(li)
'''
------------------------入栈------------------------
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
------------------------出栈------------------------
[0, 1, 2, 3]
[0, 1, 2]
[0, 1]
[0]
[]
'''

4、简单购物车

  • 要求如下:实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入
msg_dic = {
    'apple': 10,
    'tesla': 100000,
    'mac': 3000,
    'lenovo': 30000,
    'chicken': 10,
}
"""
操作比较繁琐:下面使用简洁的方式实现
shopping_list = []
while True:
    # 判断自己购物车里面有没有东西,没有东西则不打印自己的购物车,且防止下面item解压赋值报错。
    if shopping_list:
        print('购买记录'.center(50, '-'))
        for item in shopping_list:
            trade_name, spend_price, count = item
            print(f'商品【{trade_name}】 花的钱【{spend_price}】元 买了多少个【{count}】元')

    # 打印商品列表
    print('商品列表'.center(50, '-'))
    for key, value in msg_dic.items():
        print(f'商品【{key}】 价格【{value}】元')

    trade_name = input('要买啥>>:').strip()
    price = input('请花钱>>:').strip()
    count = input('要买几个>>:').strip()

    if price.isdigit() and count.isdigit() and trade_name in msg_dic:  # 用户输入都满足需求
        shopping_price = msg_dic.get(trade_name)
        price = int(price)
        count = int(count)
        if price >= shopping_price:
            count = price // shopping_price
            residue_price = price % shopping_price
            spend_price = count * shopping_price
            for item in shopping_list:
                if trade_name in item:
                    item[1] += spend_price
                    item[-1] += count
                    break
            else:
                shopping_list.append([trade_name, spend_price, count])

            if residue_price:
                print(f'来找您【{residue_price}】元')
            else:
                print('恭喜你!花光了所有钱!')
        else:
            print('穷鬼!糊弄谁呢?')
    else:
        print('非法输入用户重新输入')
"""
while True:
    # 购物购物,一上来肯定是要先打印购物信息给用户看
    print('商品列表'.center(50, '-'))
    for key, value in msg_dic.items():
        print(f'商品【{key}】 价格【{value}】元')

    # 用户输入商品名,要判断用户商品存不存在咯!
    trade_name = input('要买啥>>:').strip()
    if trade_name not in msg_dic:
        print(f'商品【{trade_name}】都不存在,买个球。')
        continue

    # 如果用户输入的个数的都不是数字,还买个毛!
    count = input('要买几个>>:').strip()
    if not count.isdigit():
        print('请输入数字!')
        continue

    # 统计用户商品名,个数,商品单价,总价.
    count = int(count)
    unit = msg_dic.get(trade_name)
    total_price = count * unit
    print((trade_name, unit, count, total_price))
    break

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

li = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
# 使用fromkeys快速造出字典(注意: 这样造出的列表,都是指向同一个内存空间)
"""
dic = {}.fromkeys(['k1', 'k2'], [])
print(id(dic['k1']), id(dic['k2']))  # 1901131209088 1901131209088
"""

"""
错误:
dic = {'k1': [], 'k2': []}
print(id(dic['k1']), id(dic['k2']))  # 1338957502592 1338957502784
number = 66
for item in li:
    if item > number:
        dic['k1'].append(item)
    else:
        dic['k2'].append(item)
print(dic)  # {'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55, 66]}
"""
dic = {'k1': [], 'k2': []}
print(id(dic['k1']), id(dic['k2']))  # 1338957502592 1338957502784
number = 66
for item in li:
    if item > number:
        dic['k1'].append(item)
    elif item < 66:
        dic['k2'].append(item)
print(dic)  # {'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55]}

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

# 利用集合去重(注意:1、集合中的元素要是不可变类型,如果是争对不可变类型的值就不能使用。2、集合去重以后是没有顺序的,如果想保留原来的数据的顺序性,就不能使用。)
"""
s = 'hello alex alex say hello sb sb'
word_list = s.split()
dic = {}
set1 = set(word_list)
for item in set1:
    word_count = word_list.count(item)
    dic.setdefault(item, word_count)
print(dic)  # {'sb': 2, 'alex': 2, 'hello': 2, 'say': 1}
"""
# 优化上面操作:上面没有必要使用setdefault,因为set1这个集合去重了以后,每次求出的word_count都是唯一的。直接使用: dic[item] = word_count就行
s = 'hello alex alex say hello sb sb'
word_list = s.split()
dic = {}
set1 = set(word_list)
for item in set1:
    word_count = word_list.count(item)
    dic[item] = word_count
print(dic)  # {'sb': 2, 'alex': 2, 'hello': 2, 'say': 1}
posted @ 2020-03-17 23:45  给你加马桶唱疏通  阅读(125)  评论(0编辑  收藏  举报