• 5月31日作业, 字典 取值,分别赋值等


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

    -----------------------------------------------

      shopping_car=[]
    # while True:
    # for k in msg:
    # info=('商品:%s 价钱:%s'%(k,msg[k]))
    # print(info.center(50," "))
    #
    # name=input('please input shopping name').strip()
    # if name not in msg:
    # print('重现输入')
    # while True :
    # count=input('选择商品数量').strip()
    # if count. isdigit:
    # coun=int(count)
    # break
    # else:
    # print('input ')
    # for item in shopping_car:
    # item["count"]+=count
    # break
    # else:
    # price=msg[name]
    # info={'name':name,'count':count,'price':price}
    # shopping_car.append(info)
    # print(shopping_car)
    ------------------------------------------------------------

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


    # for i in a:
    # if i<66:
    # c['b'].append(i)
    # else:
    # c['g'].append(i)
    # print(c)
    #
    # c={'key1':[],'key2':[]}
    # for i in a :
    # if i <66:
    # c['key1'].append(i)
    # else:
    # c['key2'].append(i)
    # print(c)

     ------------------------------------------------------------------------------------

    3. 统计s='hello alex alex say hello sb sb'中每个单词的个数
    结果如:{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}

    s='hello alex alex say hello sb sb'


    l=s.split()
    dic={}
    for item in l:
        if item in dic:
            dic[item]+=1
        else:
            dic[item]=1
    print(dic)

    4. 有如下列表,请采用两种方式取出列表中的值
     my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
     方式一:依赖索引,请写出while循环与for循环两种实现方式
     方式二:不依赖索引

    my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
    #
    # for i in my_girl_friends:
    # print(i)
    #
    # i=0
    # while i<len(my_girl_friends):
    # print(my_girl_friends[i])
    # i+=1
  • 相关阅读:
    ELK环境搭建
    django orm 操作表
    django1.11入门
    CentOS7 yum安装python3.6
    完美的【去重留一】SQL
    CentOS7安装docker
    【Jenskins】安装与配置
    【Linux】网卡配置与绑定
    【SaltStack】一些常用模块举例
    【SaltStack】通过Master给Minion安装MySQL
  • 原文地址:https://www.cnblogs.com/lijieshi/p/9119360.html
Copyright © 2020-2023  润新知