day09 作业

day 09 作业

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

a = ['alex',49,[1900,3,18]]
name,age,birth=a
year,mouth,day=birth

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

l = []
l.append(1)
l.append(2)
l.append(3)
print(l)
l.pop(0)
l.pop(0)
l.pop(0)
print(l)

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

l = []
l.append(1)
l.append(2)
l.append(3)
print(l)
l.pop()
l.pop()
l.pop()
print(l)

4、简单购物车,要求如下:

实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  

msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10,
}
while True:
    for key,value in msg_dic.items():
        #方式1
        print('商品名称:%s , 单位:%s'%(key,value))
        #方式2
        print('商品名称:{}  单位:{}'.format(key,value))
        #方式3
        print(f'商品名称:{key}  单位:{value}')
    name = input("请输入商品名称:")
    count = input("请输入商品数量:")
    if name in msg_dic:
        shopping =(
            name,msg_dic[name]*int(count),count
        )
        print(shopping)
        break
    else:
        print("输入为空或者非法字符")

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

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

a = [11,22,33,44,55,66,77,88,99,90]
a.sort()
number = 0
while True:
    if a[number]>66:
        break
    else:
        pass
    number+=1
dict1 = {}
dict1.update({"k1":a[number:],"k2":a[0:number-1]})
print(dict1)

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

s='hello alex alex say hello sb sb'
list_s = s.split(" ")
print("hello:",list_s.count("hello"))
print("alex",list_s.count("alex"))
print("say",list_s.count("say"))
print("sb",list_s.count("sb"))


#############
s='hello alex alex say hello sb sb'
list_s = s.split(" ")
dic={}
for line in new_list:
    if line not in dic:
        dic[line]:1
    else:
        dic[line]:+=1
print(dic)
原文地址:https://www.cnblogs.com/hz2lxt/p/12464591.html