Ⅷ: Job

1. A list of [ 'alex', 49, [1900,3,18]], the list of names are removed, age, birth year, month, day assigned to different variables

l = ['alex',49,[1900,3,18]]
name = l[0]
age = l[1]
birth_y = l[2][0]
birth_m = l[2][1]
birth_d = l[2][2]
print('{name} {age} {y}-{m}-{d}'.format(name=name, age=age, y=birth_y, m=birth_m, d=birth_d))

2. insert the pop queue list SIMULATION

l=[]
for i in range(1, 4):
    l.insert(i, i)
    print(l)
for i in l:
    l.pop(0)
    print(l)
l.pop(0)
print(l)

3. insert and pop the stack with a list SIMULATION

l=[]
for i in range(1, 4):
    l.insert(i, i)
    print(l)
for i in l:
    l.pop()
    print(l)
l.pop()
print(l)

4. Simple shopping cart, requirements are as follows:

Print the implementation details, the user enters the number of trade names and purchase, it will trade name, price, number of buy to add to the list of triples in the form, if the input is empty or other illegal input require the user to re-enter  

# msg_dic={
# 'apple':10,
# 'tesla':100000,
# 'mac':3000,
# 'lenovo':30000,
# 'chicken':10,
# }

shop_car = []
print(msg_dic)
while True:
    inp_name = input('请输入购买的商品名:').strip()
    inp_number = input('请输入购买的数量:').strip()
    if inp_name in msg_dic:
        t = (inp_name, msg_dic[inp_name]*int(inp_number), int(inp_number))
        shop_car.append(t)
        print('购物车列表(商品名,总价,数量):{list}'.format(list=shop_car))
    elif inp_name == '' or inp_number == '':
        print('输入为空,请重新输入')
    else:
        print('暂无该商品,请重新输入')

The following set of values ​​[11,22,33,44,55,66,77,88,99,90 ...], all values ​​greater than 66 will be saved to the first key in the dictionary, will be less than 66 value stored in the second key to a value of

That is: { 'k1': all values ​​greater than 66, 'k2': all values ​​less than 66}

l = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90]
l1 = []
l2 = []
dic={}
for i in l:
    if i > 66:
        l1.append(i)
    elif i < 66:
        l2.append(i)
dic.update({'k1': l1, 'k2': l2})
print(dic)

6. The number of each word count s = 'hello alex alex say hello sb sb' in

s = 'hello alex alex say hello sb sb'
ls = s.split(' ')
dic = {}
for i in ls:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
print(dic)

Guess you like

Origin www.cnblogs.com/qujiu/p/12465698.html