1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量
name,age,birth = ['alex',49,[1900,3,18]] year,month,day = birth print(name,age,year,month,day)
2、用列表的insert与pop方法模拟队列
list1 = [] # 入队 list1.insert(0, 'first') list1.insert(1, 'second') list1.insert(2, 'third') print(list1) # 出队 print(list1.pop(0)) print(list1.pop(0)) print(list1.pop(0))
3、 用列表的insert与pop方法模拟堆栈
list1 = [] # 入队 list1.insert(0, 'first') list1.insert(1, 'second') list1.insert(2, 'third') print(list1) # 出队 print(list1.pop(-1)) print(list1.pop(-1)) print(list1.pop(-1))
4、简单购物车,要求如下:
实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,如果输入为空或其他非法输入则要求用户重新输入
msg_dic={ 'apple':10, 'tesla':100000, 'mac':3000, 'lenovo':30000, 'chicken':10, } for goods in msg_dic.items(): print(goods) # 定义空的购物车 shopping_car = [] # 用户输入需要的商品和数量 while True: want_buy = input('请输入想购买的商品:').strip() if want_buy in msg_dic : buy_num = input('输入想购买的数量:') # 判断输入的数量是否是数字 if buy_num.isdigit(): buy_num = int(buy_num) tup_goods = (want_buy,msg_dic[want_buy],buy_num) shopping_car.append(tup_goods) print('您已将{want_buy}加入购物车'.format(want_buy = want_buy)) # 判断是否退出 going_on = input('请选择是否继续购买【Q-退出,Y-继续】:').strip() if going_on == 'Q': print(f'您选择了退出程序,购物车中包含{shopping_car}') break else: print('请继续,祝您购物愉快') else: print('请输入数字!') else: print('商品不存在')
5、有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
num = [11,22,33,44,55,66,77,88,99] dic = {'k1':[],'k2':[]} for i in num: if i < 66: dic['k1'].append(i) else: dic['k2'].append(i) print(dic)
6.统计s='hello alex alex say hello sb sb'中每个单词的个数
s='hello alex alex say hello sb sb' hello = s.count('hello') alex = s.count('alex') say = s.count('say') sb = s.count('sb') print(hello) print(alex) print(say) print(sb)