zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    微信小程序支付接口之Django后台
    wx.request 请求与django
    ubuntu16.04 安装使用meld及问题
    微信小程序上传单张或多张图片
    ip地址掩码和位数对应关系表、子网掩码、网络地址、主机地址-yellowcong
    公网IP地址就一定是A类地址和B类地址吗?那C类地址就一定是私有地址吗?
    太厉害了,终于有人能把TCP/IP协议讲的明明白白了!
    linux/shell/bash 自动输入密码或文本
    shell case例子
    spring 配置Value常量(不支持到static上)
  • 原文地址:https://www.cnblogs.com/lijieshi/p/9119360.html
Copyright © 2011-2022 走看看