I'm out of tune with the world and I'm appreciate with Python

Source: Internet
Author: User
Tags uppercase letter

1, the following variables (TU is a tuple), please implement the required functions
Tu = ("Alex", [One, one, 11, {"K1": ' V1 ', "K2": ["Age", "name"], "K3": (, 22, 33)}, 44])
0 1
0 1 2
0 1 2
A. Describing the attributes of a tuple
Print ("Tuples are immutable lists, which can put data of any data type, can be queried, can be looped, can be sliced, but cannot be changed.")
B. Can you tell me if the first element in the TU variable, "Alex", could be modified?
Print ("No, the first layer element of a tuple is not modifiable")
C. What is the value of "K2" in the TU variable? Is it possible to be modified? If you can, add an element, "Seven", to the
is a list that can be modified,
Print (Type (tu[1][2][' K2 '))
tu[1][2]["K2"].append ("Seven")
Print (TU)
D. What is the value of "K3" in the TU variable? Is it possible to be modified? If you can, add an element, "Seven", to the
Print (Type (tu[1][2][' K3 '))
corresponding to the tuple, can not be modified.

DiC = {' K1 ': "v1", "K2": "V2", "K3": [11, 22, 33]}
A. Please loop out all the keys
Print (Dic.keys ())
B. Please loop out all the value
Print (Dic.values ())
C. Please loop out all the keys and value
Print (Dic.items ())
D. Add a key-value pair in the dictionary, "K4": "V4", and output the added dictionary
dic["K4"] = "V4"
Print (DIC)
E. Please modify the dictionary "K1" corresponding to the value "Alex", Output the modified dictionary
dic[' K1 ' = "Alex"
Print (DIC)
F. Please append an element 44 to the value of the K3, and output the modified dictionary
dic["K3"] = "[11,22,33,44]"
dic["K3"].append (#dic). ["K3"] the corresponding value is [11,22,33], based on the addition of 44, using dic["K3"].append (44) can be
Print (DIC)
G. Insert element 18 in the 1th position of the K3 corresponding value, output the modified dictionary
DiC = {' K1 ': "v1", "K2": "V2", "K3": [11, 22, 33]}
Dic.get ("K3"). Insert (0,18)
dic["K3"].insert (0,18)
Print (DIC)


Question three:
Av_catalog = {
"Europe and America": {
"Www.youporn.com": ["a lot of free, the world's largest", "General quality"],
"Www.pornhub.com": ["a lot of free, also very big", "quality than Yourporn high"],
"Letmedothistoyou.com": ["mostly selfie, high-quality pictures many", "resources are not many, update slow"],
"X-art.com": ["high quality, really high", "all charges, cock wire please bypass"]
},
"Japan and Korea": {
"Tokyo-hot": ["How the quality is not clear, the individual has not liked Japan and South Korea fan", "Verygood"]
},
"Continent": {
"1024": ["All free, really good, good Life Peace", "server in foreign countries, slow"]
}
}

A, give this ["a lot of free, the world's largest", "Quality General"] list the second position inserts an element: ' Very large '.
av_catalog["Europe and America"] ["www.youporn.com"][1]= ' quantity is very big ' #要学会变通, the dictionary of this problem is Av_catalog
Print (Av_catalog)

b, will this ["high quality, really high", "all charges, the cock wire please bypass"] List of "All charges, cock wire please bypass" delete.
av_catalog["Europe and America" ["x-art.com"] =["high quality, really high"]
Print (Av_catalog)
c, in this ["High quality, really high", "all charges, cock wire please bypass"] list Add "Golden Boss favorite This".
av_catalog["Europe and America" ["X-art.com"].append ("Golden boss likes This")
Print (Av_catalog)
D, this ["How quality is unclear, the individual has not liked Japan and South Korea fan", "Verygood"] List of "Verygood" all become uppercase.
av_catalog["Japan and Korea" ["tokyo-hot"][1] = av_catalog["Japan and Korea"] [Tokyo-hot]][1].upper ()
Print (Av_catalog)
E, add a key-value pair ' 1048 ' to the dictionary corresponding to ' continent ': [' One day is sealed ']
av_catalog["Continent"][1048] = [' A day is sealed ']
Print (Av_catalog)
F, delete this "letmedothistoyou.com": ["mostly selfie, high-quality pictures many", "Resource not much, update slow"] key value pairs.
Del av_catalog["Europe and America" ["letmedothistoyou.com"]
Print (Av_catalog)
G, give this ["All free, really good, a good Life Peace", "server in Foreign, slow"] list of the first element, plus a sentence: ' Can climb down '
av_catalog["mainland" ["1024"] = [' Can climb down ', ' all free, really good, peace for the Good life ', ' server in foreign, slow ']
Print (Av_catalog)
DIC = {' name ': ' Alex ', ' age ': 18}

For I in DIC: #将字典中的元素依次遍历, the default is traversal key
Print (i) #将字典中的键打印输出
Print (Dic[i]) #将键对应的值打印输出

4. The string "K:1|k1:2|k2:3|k3:4" is processed into a dictionary {' K ': 1, ' K1 ': 2 ...} (Upgrade question)

s = "K:1|k1:2|k2:3|k3:4" #这是一个字符串
New_li = S.split ("|") #将字符串用竖线进行切割 and assign the resulting new list to the New_li on the right
DIC = {} #创建一个新的字典
Print (New_li) #打印新的列表
For i in New_li: #将列表中的每一个元素进行遍历
K,v = I.split (":") #将上面遍历的元素, cut with ":" and assign value to the left K.V
DIC[K] = Int (v) #向字典中添加键值对
Print (DIC) #打印字典

5. Element classification
The following value li= [11,22,33,44,55,66,77,88,99,90], saving all values greater than 66 to the first key in the dictionary, and saving the value less than 66 to the value of the second key.
That is: {' K1 ': List of all values greater than 66, ' K2 ': List of all values less than 66}
li= [11,22,33,44,55,66,77,88,99,90]
DIC = {}
S1 = []
s2 = []
For I in Li:
If I > 66:
S1.append (i)
Else
S2.append (i)
Dic.setdefault (' K1 ', S1)
Dic.setdefault (' K2 ', S2)
Print (DIC)

The following value li= [11,22,33,44,55,66,77,88,99,90], saving all values greater than 66 to the first key in the dictionary, and saving the value less than 66 to the value of the second key.
That is: {' K1 ': List of all values greater than 66, ' K2 ': List of all values less than 66}
li= [11,22,33,44,55,66,77,88,99,90] #列表
DiC = {' K1 ': [], ' K2 ': []} #创建一个字典
For I in Li: #将列表中的元素遍历
if i = =: #如果i = 66, stop this layer loop and continue to the next loop
Continue
Elif i >: #如果i >66
Dic.setdefault (' K1 '). Append (i) #dic. SetDefault (' K1 ') This means that the key K1 the corresponding list, adding values that meet the criteria to the list
Else
Dic.setdefault (' K2 '). Append (i) #dic. SetDefault (' K1 ') This means that the key K1 the corresponding list, adding values that meet the criteria to the list

Print (DIC)
Question 6th
‘‘‘

1: Page display serial number + product name + commodity price, such as:
1 PC 1999
2 Mouse 10
...
2: The user enters the selected product serial number, then prints the product name and the commodity price
3: If the user entered an incorrect serial number, you are prompted to enter it incorrectly and re-enter it.
4: User input Q or q, exit the program.
‘‘‘
Goods = [{"Name": "Computer", "Price": 1999}, #这是一个包含四个字典的列表
{"Name": "Mouse", "Price": 10},
{"Name": "Yacht", "Price": 20},
{"Name": "Beauty", "Price": 998},]

While 1: #无限循环
For I in Goods: #将列表中的的键依次进行遍历
Print (Goods.index (i) + 1,i[' name '],i[' price ')) #输出 (Goods.index (i) + 1) Index of the key-value pair in the list +1, the value corresponding to the output key
#name和price都是字典中的键
Str_input = input (' Please enter the item number you want to select, enter q/q exit: ') #创建一个用户交互

If Str_input.isdigit () and 0 < int (str_input) < Len (goods): #如果输入的内容是数字, and the number is greater than 0, less than the length of the list
i_index = Int (str_input)-1 #将用户输入的字符串变成整型, the number minus 1 becomes the corresponding index, assigns the value to the left I_index
Print (goods[i_index][' name '],goods[i_index][' price ') #打印列表中索引对应的字典中的商品名称, and prices
Elif str_input.upper () = = ' Q ': #如果输入的字母是Q, regardless of whether the user enters an uppercase or lowercase letter, it becomes an uppercase letter,
Break #终止循环
Else
Print (' Input wrong, please re-enter! ')

I'm out of tune with the world and I'm appreciate with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.