Dictionary DIC (CRUD nested)

How to reverse a string? Such as: name = "wupeiqi" Please inverted to name = "iqiepuw"

name = "wupeiqi"
name1=name[::-1]
print(name1)

How "1,2,3" becomes [ '1', '2', '3']

name = '1,2,3'
name1=name.split(',')
print(name1)

name = '1,2,3'
l1=[]
name1=name.replace(',','')
for i in name1:
  l1.append(i)
print(l1)

How [ '1', '2', '3'] becomes [1,2,3]

l1=['1','2','3']
l2=[]
for i in l1:
  l2.append(int(i))
print(l2)

The following code is what the output list = () [ 'a', 'b', 'c', 'd', 'e']) print list? [10:]

[]

A list of existing alist, please write method repeats of two elements alist removed, wherein :( difficult ) required to maintain the original order in which elements of the list.

alist=[1,1,1,2,3,4,5,65,4,5,4,6,9,]
alist1=[]
for i in alist:
  if i not in alist1:
      alist1.append(i)
print(alist1)

Without regard to the order of the original elements of the list ( Chaogang ).

Achieve 99 multiplication table (in Python hard )

 

Please list each element by "_" link up.

users = [ 'Lishao Qi' 'Li sail', 'slag slag Hui'] 
users = [ 'Lishao Qi' 'Li sail', 'slag slag Hui']
users1 = '_'. the Join (Users)
Print (users1)

Please list each element by "_" link up.

users = [ 'Lishao Qi' 'Li sail', 666 'slag slag Hui'] 
users = [ 'Lishao Qi' 'Li sail', 666 'slag slag Hui']
users1 = []
for I in Users:
  users1.append (STR (I))
users1 = '_'. the Join (users1)
Print (users1)

Please tuples v1 = all elements (11,22,33) is added to the list of v2 = [44,55,66] in.

v1 = (11, 22, 33)
v2 = [44,55,66]
v2.extend(v2)
print(v2)

v1 = (11, 22, 33)
v2 = [44,55,66]
for i in v1:
  v2.append(i)
print(v2)

V1 = set of all tuples in (11,22,33,44,55,66,77,88,99) even index position of the element added to the list of v2 = [44,55,66] in.

v1 = (11,22,33,44,55,66,77,88,99)
v2 = [44,55,66]
for i in range(len(v1)):
    if i%2==0:
        v2.append(v1[i])
print(v2)

The dictionary keys and values ​​added to the key_list and value_list two lists, such as:

key_list = []
value_list = []
info = {'k1':'v1','k2':'v2','k3':'v3'}
for i in info.keys():
    key_list.append(i)
for j in info.values():
    value_list.append(j)
print(key_list)
print(value_list)

字典dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}

a. Please recycle all of the key output
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
for i in dic.keys():
    print(i)
b. Please recycle all of the output value
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
for i in dic.values():
    print(i)
c. Please recycle all of the key and the output value
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
for kay,val in dic.items():
    print (kay, val)
. D add a key-value pair in the dictionary, "k4": "v4", added after the output dictionary
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
dic['k4']='v4'
print(dic)
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
dic.setdefault('k4','v4')
print(dic)

e. Please after the dictionary, and outputs modified "k1" corresponds to a value "alex" in the dictionary modification
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
dic.setdefault('k4','v4')
print(dic)
 Append a dictionary element 44, the output value corresponding modifications k3 after the
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
dic['k3'].append(44)
print(dic)
Insert elements 18, the output of a modified dictionary k3 values ​​corresponding to the position
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
dic['k3'].append(44)
print(dic)

 

av_catalog = {
    "Europe": {
        ". Www Bai .com": [ "a lot of free, the world's largest", "quality in general"],
        "Www.alex.com": [ "a lot of free, is also great," "very good quality"],
        "Oldboy.com": [ "mostly selfie, a lot of high-quality images," "limited resources, updating slow"],
        "Hao222.com": [ "high quality, really high," "all charges, please bypass the Cock wire"]
    },
    "Japan and South Korea": ​​{
        "Tokyo-hot": [ "What quality do not know, personally do not like Japan and South Korea have been the norm", "verygood"]
    },
    "mainland":{
        "1024": [ "all free, good, good life of peace", "server in a foreign country, slow"]
    }
}

To this [ "a lot of free, the world's largest", "quality is generally"] list of second position to insert an element: 'significant amount'.

av_catalog [ "Western"] [ "www. Bai .com"]. insert (2, 'large amount')
print (av_catalog)

This [ "high quality, really high," "all charges, please bypass Cock wire"] list of "all charges, Cock wire bypass the" Remove.

av_catalog["欧美"]["hao222.com"].pop(1)
print (av_catalog)

This [ "Quality is not clear how the individual is no longer like the Japanese and Korean Fan", "verygood"] list "verygood" All capitalized.

av_catalog["日韩"]["tokyo-hot"][-1]='VVVVV'
print (av_catalog)

To the 'mainland' corresponding dictionary to add a key-value pair '1048': [ 'day closure']

av_catalog [ "mainland"] [ "1048"] = [ "day closure ']
print (av_catalog)

Delete this key-value pair: "oldboy.com": [ "mostly selfie, a lot of high-quality images," "limited resources, slow to update."]

To this [ "all free, good, good life of peace", "server in a foreign country, slow"] The first element of the list, add a sentence: 'can climb down'

Please print each cycle k2 corresponding element values.

info = {
    'K1', 'v1',
    'k2':[('alex'),('wupeiqi'),('oldboy')],
}

A character string "k: 1 | k1: 2 | k2: 3 | k3: 4" processed into a dictionary { 'k': 1, 'k1': 2 ....}

str1="k: 1|k1:2|k2:3 |k3 :4"
str1=str1.split('|')
dic = {}
for i in str1:
    i1 = i.split ( ':')
    dic[i1[0]]=i1[1]
print(dic)

 

 

  1. write the code

    """
    Have the following values ​​li = [11,22,33,44,55,66,77,88,99,90], all values ​​greater than 66 will be saved to a list of the first dictionary corresponding to the key, will be less than 66 stored value to a second list corresponding to the key.
    
    result = {'k1':[],'k2':[]}
    """
    
    = [11,22,33,44,55,66,77,88,99,90]
    result = {'k1':[],'k2':[]}
    for i in li:
        if i>66:
            result['k1'].append(i)
        elif i<66:
            result['k2'].append(i)
    print(result)
    
    Output: { 'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55]}
    """
    
  2. Output of goods list, the user enters the serial number, the user selects the display of goods

    """
    Product List:
      goods = [
    		{"name": "电脑", "price": 1999},
    		{"name": "鼠标", "price": 10},
    		{"name": "游艇", "price": 20},
    		{"name": "美女", "price": 998}
    	]
    Claim:
    1: The page number display + Product Name + commodity prices, such as:
          1 PC 1999 
          2 mouse 10
    for i in range(len(goods)):
        print(i+1,goods[i]['name'],goods[i]['price'])	  
    	  
    User input selection of goods number, then print Commodity Description and commodity prices
    for i in range(len(goods)):
        print(i+1,goods[i]['name'],goods[i]['price'])
    my_inpuy = input ( 'Enter the number of goods purchased')
    print ( 'are you purchased% s, the amount is% s'% (goods [int (my_inpuy) -1] [ 'name'], goods [int (my_inpuy) -1] [ 'price']))
    
    3: If the user enters the product number is incorrect, you are prompted an error, and re-enter.
    4: a user input Q or q, exit the program.
    while 1:
        goods = [
                {"name": "电脑", "price": 1999},
                {"name": "鼠标", "price": 10},
                {"name": "游艇", "price": 20},
                {"name": "美女", "price": 998}]
        for i in range(len(goods)):
            print(i+1,goods[i]['name'],goods[i]['price'])
        my_input = input ( 'Enter the number of goods purchased')
        if my_input.isdecimal():
            if int(my_input)>len(goods):
                print ( 'the number you entered is too big')
            elif 1<= int(my_input)<=len(goods):
                print ( 'are you purchased% s, the amount is% s'% (goods [int (my_input) -1] [ 'name'], goods [int (my_input) -1] [ 'price']))
            else:
                print ( 'You have entered incorrect')
        elif my_input.upper()=='A':
            print ( 'exit')
            break
        else:
            print ( 'Enter a number ooooooooooooooooo pro')
    """
    
    
  3. Look at the code written results

    v = {}
    for index in range(10):#[1---9]
        v['users'] = index
    print (v)
    #{'users': 9}
    
# Print the number of rows outer loop
* Number of inner loop Print #  
# For nested while
# count=1
# for i in range(1,6):
#     while count==i:
#         print('*'*i)
#         count+=1

#for for nesting
# for i in range(1,6):
#     for j in range(1,i+1):
#         if i==j:
#          print(j*'*',end='')
#     print()

# While for nesting
# count=1
# while count<6:
#     for i in range(count+1):
#         if i==count:
#             print('*'*i)
#     count+=1

#while 嵌套 while
# i,j=1,1
# while i<7:
#     while j<i:
#         print('*'*j)
#         j+=1
#     i+=1
 @Wang Sir 

Guess you like

Origin www.cnblogs.com/saoqiang/p/11019251.html