作业

1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量
l = ['alex', 49, [1900, 3, 18]]
name, age, birth = l
year,month,day=birth
2、用列表的insert与pop方法模拟从列表的一端存值,然后从另外一端取值
l = [111,222,333,444,555]
l.insert(1,123)
print(l)
res = l.pop(-1)
print(res)

4、简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入  
msg_dic = {
'apple': 10,
'tesla': 100000,
'mac': 3000,
'lenovo': 30000,
'chicken': 10,
}
goods_1 = []
while True:
for k,v in msg_dic.items():
print("name:{name} price:{price}".format(name=k,price=v))
choice = input("商品>>:").strip()
if not choice or choice not in msg_dic:
continue
count = input("购买个数>>:").strip()
if not count.isdigit():
continue
goods_1.append((choice,msg_dic[choice],count))
print(goods_1)
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]
l1 = {"k1":[],"k2":[]}

for x in l:
if x > 66:
l1["k1"].append(x)
else:
l1["k2"].append(x)
print(l1)

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

s = 'hello alex alex say hello sb sb'

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

posted @ 2020-07-13 20:26  刘海子  阅读(15)  评论(0)    收藏  举报