Python data type

Source: Internet
Author: User

Requirements: You can choose to go to each submenu from any level back to the previous layer you can exit the program from any level required new knowledge points: List, dictionary

Menu = {
' Beijing ': {
' Haidian ': {
' Five crossing ': {
' Soho ': {},
' NetEase ': {},
' Google ': {}
},
' Zhongguancun ': {
' Iqiyi art ': {},
' Autohome ': {},
' Youku ': {},
},
' On the ground ': {
' Baidu ': {},
},
},
' Changping ': {
' Shahe ': {
' Old boy ': {},
' Beihang University ': {},
},
' Tian Tong Yuan ': {},
' Huilongguan ': {},
},
' Chaoyang ': {},
' Dongcheng ': {},
},
' Shanghai ': {
' Minhang ': {
"People's Square": {
' Fried Chicken Shop ': {}
}
},
' Zhabei ': {
' Train war ': {
' Ctrip ': {}
}
},
' Pudong ': {},
},
' Shandong ': {},
}

Current_layer = Menu
Last_layer = []

While True:
For I in Current_layer:
Print (i)
Choice = input ("Enter your location:")
If choice in Current_layer:
Last_layer.append (Current_layer) #保存当前层
Print (Current_layer)
Current_layer = Current_layer[choice]
elif Choice = = ' B ' or choice = = ' B ':
If Len (last_layer)! = 0:
Current_layer = Last_layer.pop () #删除最后一层
else:
Print (' Already top of the story ')
elif Choice = = ' Q ' or choice = = ' Q ':
Print (' Exit selection ')
Break
else:
Print (' Enter the correct location ')

PS: How is this defined as a function implementation? To be modified later

1. List
A list is a collection of data, which can be placed in any data type, can be added to the collection of search and deletion;
Name=[' A ', ' B ', ' C ', ' d ', ' f ', ' d ', ' G ']

        1. Query element Name[0] Find the first element by index name[-1]z find the last element Name[-2] The penultimate element name.index (' d ') returns the index value of the specified element, from        Look around, find the first match; Name.count (' d ') counts the number of element D; 2. Slice name[0:3] Take 0-2 elements, namely the first three, slicing Gu Tou ignoring the tail name[:3] take the first three   Name[-5:] Take the last five name[::] Take all elements name[:5:2] Take 1, 3, 5; 2 is the step size; take one element every 2; 3. Add name.append (' haha ')        Append an element to the end of haha 4. Insert Name.insert (0, ' Abd ') position 0 Insert Element Abd name.insert (2, ' AA ') position 2 insert Element AA 5. Modify        N[3] = ' Hello ' fourth element modified to Hello 6. Delete Name.pop Delete the last Name.remove (' a ') delete element A, from left to right, delete the first element a Del name[2] Delete the element with index 2; (Del is a global delete command that can delete all, including variables, etc.) del Name[2:4] Delete the element of index 2-3 7. Loop L2 = [' A ', ' B ', ' C ', ' d ', ' e ', ' F '] for I in L2:print (i) assigns each value in the L2 list to the variable i, printing out the value of each I; a range (10) generates a 0 The list to 9 for I in range (10) 8. Sort Name.sort () sorts the name according to ASCII from large to small, and then looks at the list after sorting; name Name.re      Verse () will list name flashback  9. Merge list name+name1 the name and name1 list merge Name.extend (name1) the list name extension 10. Clear List Name.clear () clear   Empty Name list 11.name1=name Name1=name the Name1,name list is modified, name1 data is synchronized 12.copy name1=name.copy () #浅copy Import Copy #深copy, imports the Python toolbox name1=name.deepcopy (name) 13.enumerate (name) #枚举

2. Dictionaries
The dictionary is unordered;
dic={' K1 ': ' v1 ', ' K2 ': ' V2 '}
Modify
dic[' K1 ']=2 #修改
dic[' K3 ']= ' v3 ' #增加

3. Tuples
tuple = (' A ', ' B ', ' C ')
Tuples cannot be modified

Python data type

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.