# 1、有列表['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日赋值给不同的变量
l = ['alex', 49, [1900, 3, 18]] name = l[0] age = l[1] year, month, day = l[2]
# 2、用列表的insert与pop方法模拟队列l = []# 入队操作l.insert(0, 1)l.insert(1, 2)l.insert(2, 3)# 出队操作l.pop(0)l.pop(0)l.pop(0)# 3. 用列表的insert与pop方法模拟堆栈l = []# 入栈操作l.insert(0, 1)l.insert(1, 2)l.insert(2, 3)# 出栈操作l.pop()l.pop()l.pop()# 4、简单购物车,要求如下:# 实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数以三元组形式加入购物列表,# 如果输入为空或其他非法输入则要求用户重新输入
msg_dic={
'apple':10,
'tesla':100000,
'mac':3000,
'lenovo':30000,
'chicken':10}
print(msg_dic)
shopping = []
while True:
name = input('请输入商品名称:').strip()
if not name:
continue
if name in msg_dic:
number = input('请输入商品购买个数:').strip()
if not number:
continue
if number.isdigit():
shopping_tuple = (name, msg_dic[name], int(number))
shopping.append(shopping_tuple)
print('您购买的商品%s以加入购物车' % name)
print('购物车的商品有:%s' % shopping)
else:
print('非法输入')
continue
else:
print('输入的商品不存在')
continue
# 5、有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,# 将小于 66 的值保存至第二个key的值中# 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
lis = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
dict_l = {'k1': [], 'k2': []}
for i in lis:
if i > 66:
dict_l['k1'].append(i)
else:
dict_l['k2'].append(i)
# print(dict_l)
# 6、统计s='hello alex alex say hello sb sb'中每个单词的个数
s = 'hello alex alex say hello sb sb'
s_list = s.split()
s_dict = {}.fromkeys(s_list, 0)
for i in s_list:
if i in s_dict:
s_dict[i] += 1
print(s_dict)
来源:https://www.cnblogs.com/xiaolang666/p/12464786.html