zoukankan      html  css  js  c++  java
  • python学习之元组列表操作作业

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

    name,age,birth_day = ['alex',49,[1900,3,18]]
    print(name,age,birth_day)
    

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

    list01 = []
    list01.insert(0,'first')
    list01.insert(1,'second')
    list01.insert(2,'third')
    print(list01)        #['first', 'second', 'third']
    print(list01.pop(0)) #firt
    print(list01.pop(0)) #second
    print(list01.pop(0)) #third
    

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

    list01 = []
    list01.insert(0,'first')
    list01.insert(1,'second')
    list01.insert(2,'third')
    print(list01)       #['first', 'second', 'third']
    print(list01.pop()) #third
    print(list01.pop()) #second
    print(list01.pop()) #first
    

    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
    }
    goods_list = []
    inp_name = input('请输入购买的商品名:').strip()
    inp_num = input('请输入购买的个数:').strip()
    if inp_name in msg_dic and inp_num.isdigit():
        item = (inp_name,msg_dic.get(inp_name),int(inp_num))
        goods_list.append(item)
        print(goods_list)
    else:
        print('输入有误,请重新输入。')
    

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

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

    l = [11,22,33,44,55,66,77,88,99,90]
    position = l.index(66)
    dic1 = dict(k1=l[position+1:],k2=l[:position])
    print(dic1)
    

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

    s = 'hello alex alex say hello sb sb'
    l = s.split()
    dic1 = {}
    for item in l:
        dic1.setdefault(item,0)
        dic1[item] += 1   
    print(dic1)
    
  • 相关阅读:
    前后端数据处理+数据展示分页
    数据库表关系:多对多的三中方式
    MTV与MVC模式
    F与Q查询
    ORM表单操作
    IIS 7 应用程序池自动回收关闭的解决方案
    ASP.NET MVC 使用带有短横线的html Attributes
    能加载文件或程序集“XXX”或它的某一个依赖项,系统找不到指定的文件
    调试MVC项目,不关闭 IIS EXPRESS
    已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
  • 原文地址:https://www.cnblogs.com/leilijian/p/12463742.html
Copyright © 2011-2022 走看看