作业

1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量
res = ['alex', 49, [1900, 3, 18]]
name = res[0]
age = res[1]
n = res[-1][0]
y = res[-1][1]
r = res[-1][-1]

print(name, age, n, y, r)

2、用列表的insert与pop方法模拟队列
l = []
l.insert(0, 'aaa')
l.insert(1, 'bbb')
l.insert(2, 'ccc')
print(l)
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))

3. 用列表的insert与pop方法模拟堆栈
l = []
l.insert(0, 'aaa')
l.insert(1, 'bbb')
l.insert(2, 'ccc')
print(l)
print(l.pop())
print(l.pop())
print(l.pop())

4、简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,
如果输入为空或其他非法输入则要求用户重新输入  
msg_dic = {
'apple': 10,
'tesla': 100000,
'mac': 3000,
'lenovo': 30000,
'chicken': 10,
}
lis = []
while True:
name = input('输入商品名称,退出请输入n')
if name == 'n':
print('购物结束')
break
elif name in msg_dic.keys():
print('商品:{i},价格{j}'.format(i=name, j=msg_dic[name]))
while True:
number = input('请输入数量')
if number.isdigit():
res = (name, msg_dic[name], number)
lis.append(res)
print(lis)
break
else:
print('请从新输入商品名')

5、有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
res = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
dic = {'K1': [], 'K2': []}
while len(res):
lis = res.pop()
if lis > 66:
dic['K1'].append(lis)
else:
dic['K2'].append(lis)
print(dic)
6、统计s='hello alex alex say hello sb sb'中每个单词的个数
s = 'hello alex alex say hello sb sb'
count= {}
for i in s.split():
x = s.count(i)
count[i] = x
print(count)

posted @ 2020-03-11 23:02  疯狂的小左子  阅读(109)  评论(0)    收藏  举报