作业5
1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量
lsg=['alex',49,[1900,3,18]]
nale=lsg[0]
age=lsg[1]
birthday=lsg[2]
print(nale,age,birthday)
2、用列表的insert与pop方法模拟队列
l=[]
l.insert(0,1)
l.insert(1,2)
l.insert(2,3)
l.insert(3,4)
print(l)
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))
- 用列表的insert与pop方法模拟堆栈
l=[]
l.insert(0,1)
l.insert(1,2)
l.insert(2,3)
l.insert(3,4)
print(l)
print(l.pop(-1))
print(l.pop(-1))
print(l.pop(-1))
print(l.pop(-1))
4、简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入
1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量
lsg=['alex',49,[1900,3,18]]
nale=lsg[0]
age=lsg[1]
birthday=lsg[2]
print(nale,age,birthday)
2、用列表的insert与pop方法模拟队列
l=[]
l.insert(0,1)
l.insert(1,2)
l.insert(2,3)
l.insert(3,4)
print(l)
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))
print(l.pop(0))
- 用列表的insert与pop方法模拟堆栈
l=[]
l.insert(0,1)
l.insert(1,2)
l.insert(2,3)
l.insert(3,4)
print(l)
print(l.pop(-1))
print(l.pop(-1))
print(l.pop(-1))
print(l.pop(-1))
4、简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入
lsg_dic={
'apple':10,
'tesla':100000,
'lac':3000,
'lenovo':30000,
'chicken':10,
}
goods_l=[]
while True:
for key,value in msg_dic.items():
print('Name:{name} Price:{price}'.format(price=value,name=key))
choice=input('Your Goods>>: ').strip()
if len(choice) == 0 or choice not in msg_dic:
continue
count=input('Your Goods count>>: ').strip()
if count.isdigit():
count=int(count)
goods_l.append((choice,msg_dic[choice],count))
print(goods_l)
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]
m={
'k1':[],
'k2':[]
}
for i in a:
if i>66:
m['k1'].append(i)
else:
m['k2'].append(i)
print(m)
6、统计s='hello alex alex say hello sb sb'中每个单词的个数
s='hello alex alex say hello sb sb'
s=s.split()
hello=s.count('hello')
alex=s.count('alex')
say=s.count('say')
sb=s.count('sb')
print(hello,alex,say,sb)
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]
m={
'k1':[],
'k2':[]
}
for i in a:
if i>66:
m['k1'].append(i)
else:
m['k2'].append(i)
print(m)
6、统计s='hello alex alex say hello sb sb'中每个单词的个数
s='hello alex alex say hello sb sb'
s=s.split()
hello=s.count('hello')
alex=s.count('alex')
say=s.count('say')
sb=s.count('sb')
print(hello,alex,say,sb)

浙公网安备 33010602011771号