• 作业5


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

    list1 = ['alex', 49, [1900, 3, 18]]
    name = list1[0]
    age = list1[1]
    year = list1[2][0]
    month = list1[2][1]
    day = list1[2][2]
    print(name, age, year, month, day)

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

    # 队列为先进先出
    list1 = []
    list1.insert(len(list1), 1)
    list1.insert(len(list1), 2)
    list1.insert(len(list1), 3)
    print(list1)
    
    print(list1.pop(0))
    print(list1.pop(0))
    print(list1.pop(0))
    print(list1)

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

    # 堆栈为后进先出
    list1 = []
    list1.insert(len(list1), 1)
    list1.insert(len(list1), 2)
    list1.insert(len(list1), 3)
    print(list1)
    
    print(list1.pop())
    print(list1.pop())
    print(list1.pop())
    print(list1)

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

    msg_dic = {
        'apple': 10,
        'tesla': 100000,
        'mac': 3000,
        'lenovo': 30000,
        'chicken': 10,
    }
    shopping_list = []
    
    while 1:
        print("商品清单:")
        for k, v in msg_dic.items():
            print(k.ljust(15, "-"), "{}元/个".format(v))
        if shopping_list:
            sum_price = 0
            print("购物清单:")
            for i in shopping_list:
                print(i)
                sum_price += i[1]
            print("总价为:", sum_price)
        obj = input("请输入要购买的商品名:").strip()
        if obj in msg_dic:
            num = input("请输入要购买的数量:").strip()
            if num.isdigit():
                num = int(num)
            else:
                print("购买数量必须为数字")
                continue
        else:
            print("不是合法的商品名,请重新输入")
            continue
        shopping_list.append((obj, num * msg_dic[obj], num))

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

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

    list1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
    d = {"k1": [], "k2": []}
    for i in list1:
        if i > 66:
            d["k1"].append(i)
        elif i < 66:
            d["k2"].append(i)
    print(d)

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

    # 单词个数
    s = 'hello alex alex say hello sb sb'
    list1 = s.split(" ")
    d = {}
    for i in list1:
        d[i] = s.count(i)
    print(d)
    
    
    
    # 字母个数
    s = 'hello alex alex say hello sb sb'
    d = {}
    for i in s:
        d[i] = s.count(i)
    print(d)
  • 相关阅读:
    七月随想
    曾经的诗
    Foundry feats. MultiverseStudio
    "BLAME" is out.
    去除partner页面消息 自动添加关注者的功能
    Odoo小数精度及货币精度详解
    CentOs 6.x 升级 Python 版本【转】
    django template if return false
    Odoo “坑” 系列之 XML中的布尔类型
    Qweb报表中的Reload from Attachment参数
  • 原文地址:https://www.cnblogs.com/achai222/p/12463274.html
Copyright © 2020-2023  润新知