Given a set (string) of data, according to the number entered, query the place of attribution
1 defnum_info (num):2Info0 ="""5583|1860100|010| BEIJING China Unicom GSM card3 5584|1368141|010| Beijing Mobile Shenzhou Travel card4 15046|1391897|021| Shanghai Mobile Global Pass Card5 17250|1502207|022| Tianjin | Tianjin Mobile Global PASS card"""6 #according to the above string, each personal information end has a "card" word, we can follow the word to cut the string, you can also follow the line to cut7 #(If you do not wrap the string, I think it is very good to follow the word "card")8 9Lt0 = List (Info0.split ("Card"))Ten #The given string is cut by the word "card", converted to a list, assigned to Lt0 One ADict0 = {} - #Create an empty dictionary (use below) - the forIinchRange (len (lt0)-1): - #use I to iterate over the length of the Lt0 list (why reduce the length by one?). Because according to the word "card" to cut, the list is labeled-1 of the element is empty) - #The following element subscript, if not minus one, then the last element is empty, once the subscript is used to traverse to the last element, - #it will be an error (because the last element in the list is empty: You can try it and print it out to see it) + - #The following line of code is the element in the cut list, followed by the ' | ' To cut, the first element of the list is cut to the assigned LT1 (list) +LT1 = Lt0[i].split ("|") A at #Add a Dictionary key (enter the element labeled 1 under the LT1 list), - #and the value of the dictionary (the last element of the LT1 list, subscript 1, because the card is cut off when cut, add it here) -DICT0[LT1[1]] ="{} card". Format (lt1[-1]) - - #Print it Out (use the dictionary's Get function to query the number of queries, and if not, return the default value None, where you can customize the return content) - Print("the number you are querying belongs to: {}". Format (Dict0.get ("{}". Format (num),"the queried number does not exist! "))) inNUM1 = input ("Please enter the first seven digits of the number you want to inquire:") -Num_info (NUM1)
If there are errors, thank you for correcting me!
Python Essay--Query the attribution according to the number