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)
    
  • 相关阅读:
    【转】Loadrunder场景设计篇——添加windows Resource计数器和指标说明
    【转】基于Selenium的web自动化框架(python)
    测试用例
    向SQL Server中导入Excel的数据
    SQl server更新某阶段的匹配关系。
    Python -- print(dataframe)时,省略部分列。
    Python -- Pandas介绍及简单实用【转】
    sqlserver清除缓存,记录查询时间
    ArcMap 10.2 crashes during Loading Document launch stage
    PYTHON:HTTP头设置工具(以附件名为例)
  • 原文地址:https://www.cnblogs.com/leilijian/p/12463742.html
Copyright © 2011-2022 走看看