Dictionary and a list of built-in method to practice

Dictionary and a list of built-in method to practice

# 1, there is a list of [ 'alex', 49, [ 1900,3,18]], the list of names are removed, age, birth year, month, day assigned to different variables 
identifier_list = [ ' alex ' , 49 , [1900,3,18 ]]
name, age, birthday = identifier_list
birth_year, birth_month, birth_date = birthday
print(name, age, birthday, birth_year, birth_month, birth_date,end="\n\n")

# 2, and insert a simulated queue list pop method

# Queue Method a: 
Queue = []
 # enqueue insert function returns no value 
Print (Queue)
queue.insert (len (Queue), " . 1 " )         # queue.insert (len (Queue), ". 1") returns a value of None 
Print (Queue)
queue.insert(len(queue),"2")
print(queue)
queue.insert (len (Queue), " 3 " )
 Print (Queue)
 # the team: .pop function return value type for deletion of elements 
queue.pop (0)                        # queue.pop (0) return value is a list of an element of action: deleting the first element 
Print (Queue)
queue.pop(0)
print(queue)
queue.pop(0)
print(queue,end="\n\n")

# Queue Act II :( second method is more intuitive, similar to a channel) 
Queue = []
 # enqueued: 
Print (Queue)
queue.insert(0,"1")
print(queue)
queue.insert(0,"2")
print(queue)
queue.insert(0,"3")
print(queue)
#出队
queue.pop()
print(queue)
queue.pop()
print(queue)
queue.pop()
print(queue,end= "\n\n")

# 3. Simulation method pop the stack and insert the list 
Stack = []
 # push 
Print (Stack)
stack.insert(len(stack),"1")
print(stack)
stack.insert(len(stack),"2")
print(stack)
stack.insert(len(stack),"3")
print(stack)
# 出栈
stack.pop()
print(stack)
stack.pop()
print(stack)
stack.pop()
print(stack,end="\n\n")



# 4, simple shopping cart, requirements are as follows: 
# implementation details print product, 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 user input is required to re-enter   
msg_dic = {
 ' Apple ' : 10 ,
 ' Tesla ' : 100000 ,
 ' MAC ' : 3000 ,
 ' Lenovo ' : 30000 ,
 ' Chicken ' : 10 ,
}
list = []
while True:
    goods_name = the INPUT ( " Please enter the product name (output Q to quit): " )
     IF goods_name == " q "  or goods_name == " Q " :
         Print ( " ! Welcome to the next visit " )
         BREAK 
    goods_num = the INPUT ( " Please enter quantity: " )
     IF goods_name in msg_dic and goods_num.isdigit ():
        Surely = int (goods_num)
        good_1 = (goods_name, num, num*msg_dic.get(goods_name))
        list.append(good_1)
        Print (f " your current shopping list: {List} \ the n- " )
     the else :
         Print ( " input is incorrect, please re-enter \ the n-! " )
         Print (f " your current shopping list: {List} " )



# 5, the following values of the set [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 the stored value 66 to a value of the second key 
# namely: { 'k1': all values greater than 66, 'k2': all values less than 66} 
DIC = {}
list = [11,22,33,44,55,66,77,88,99,90]
dic.setdefault ( " K1 " , [])          # here can not be written dic.setdefault ( "k1", None) , the initial value must be the empty list, otherwise can not call .append () 
dic.setdefault ( " K2 " , [ ])
 for i in List:
     IF i> 66 :
        DIC [ " K1 " ] .append (I)      # If no dic.setdefault ( "k1", [] ), such an approach is incorrect then 
    the else :
        dic["k2"].append(i)
print(dic)


# 6, counting the number s = 'hello alex alex say hello sb sb' of each word 
S = ' Hello Alex Alex say Hello SB SB ' 
s_list = s.split ()
dic = {}
for i in s_list:
    if i in dic:
        dic[i] += 1
    else:
        I say [i] = 1
 print (say)

 

Guess you like

Origin www.cnblogs.com/zhubincheng/p/12462827.html