Python "Learning Path 03" collection series with multi-level menu Demo

Source: Internet
Author: User

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# @Time: 2017/11/18 9:50
# @Author: mixiu26

# Collection: Set----> Unique, unordered
List1 = [1,4,5,6,7,2,4,6]
# list to go:---->> directly convert the list to a collection: Set (List1)
List1 = set (List1)
# Print List1 and type:
Print (List1,type (list1)) # {1, 2, 4, 5, 6, 7} <class ' Set ' > set unordered, Element not duplicated

# Set intersection: Intersection ()----->> intersection
List2 = set ([2,3,4,5,6])
print (LIST1,LIST2) # {1, 2, 4, 5, 6, 7} {2, 3, 4, 5, 6}
Print (List1.intersection (list2)) # {2, 4, 5, 6}

# Set Merge set: Merge de-weight: Union ()
Print (List1.union (list2)) # {1, 2, 3, 4, 5, 6, 7}

# Difference set: Difference ()
Print (List1.difference (list2)) # {1, 7} takes out all 1 of the elements that are not in 2, takes out all the callers, and the elements that are not in the comparator
Print (List2.difference (list1)) # {3} Take out all 2 of the elements that are not in the

# Subset: Issubset----->> is part of you
Print (List2.issubset (list1)) # False List2 is a subset of List1?
list3 = set ([2,4,5,6,7])
Print (List3.issubset (list1)) # True List3 is a subset of List1:

# Parent set: Issuperset:
Print (List1.issuperset (LIST3)) # True List1 is a parent set of 3
Print (List1.issuperset (list2)) # False List1 is not a parent set of 2

# Reverse Difference set: Take out each other without each other's elements: is only the other side of the elements, all taken out
Print (List1.symmetric_difference (list2)) # {1, 3, 7}//Remove part of a two-person intersection---->> complement

list4 = set ([11,22,33,44,55])
# Determine if there is an intersection in the two collection:
Print (List1.isdisjoint (List2)) # False The intersection between the two returns false
Print (List1.isdisjoint (LIST4)) # True Returns True if there is no intersection between the two

# Set notation: & 1 ^-Orthogonal replenishment
print (LIST1,LIST2)
print (List1 & List2) # {2, 4, 5, 6}
Print (List1 | list2) # {1, 2, 3, 4, 5, 6, 7}
Print (list1 ^ list2) # {1, 3, 7}
print (LIST1-LIST2) # {1, 7}

# Collection---->> additions and changes:
List1.add (111)
print (List1) # {1, 2, 4, 5, 6, 7, 111}

# Add elements in bulk:
# list1.update (11,22,33) needs to be added in the format of the collection and cannot be added in bulk by adding a single element:
list1.update ([11,22,33])
print (List1) # {1, 2, 4, 5, 6, 7, one, 111,}--->> withdrawal disorder.

# Delete element:
List1.remove (one)
print (List1) # {1, 2, 4, 5, 6, 7, 111, $}

# Print Set length, number of elements:
print (len (list1)) # 9

# Determines whether an element is used in a collection, list, dictionary: x in a---->a can be a list, a collection string
print (one in list1) # False
print (111 in list1) # True

# POP ()--->> randomly deletes an element and returns:
# Print (List1.pop ()) # 1

# Discard---->> Delete the specified element:
Print (List1.discard (1), List1) # None {2, 33, 4, 5, 6, 7, 111, 22} must carry element Delete, delete element only, remove deleted element after delete, remove delete no element will error, Discar D not
Print (List1.discard (9876), List1) # None {2, 33, 4, 5, 6, 7, 111, 22}

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# @Time: 2017/11/18 10:55
# @Author: mixiu26

data = {
' Beijing ': {
' changping ': {
' Shahe ': [' old boy ', ' Test '],
' Tian Tong Yuan ': ["Chain Home Property", "I love my Home"]
},
' Chaoyang ': {
"Wangjing": ["Mercedes", "Mo Mo"],
" Guomao": {"CICC", "HP"},
"Dongzhimen": {"Advent", "Fetion"},
},
' Hai ding ': {},
},
' Shanghai ': {
' Minhang ': {
"People's Square": {
' Fried Chicken Shop ': {}
}
},
' Zhabei ': {
' Train war ': {
' Ctrip ': {}
}
},
' Pudong ': {},
},
' Shandong ': {
"Texas": {},
"Qingdao": {},
"Jinan": {}
}
}

Exit_flag = False

While not Exit_flag:
For i in data:
print (i)
choice = input ("Please select a menu city:")
# Determine if the selected city is in the two-level menu list range:
if choice in data:
While not exit_flag: # If you enter the city no longer my list range, I can cycle through the city list
For I2 in Data[choice]:
print ("\ t" + I2)
# Continue waiting for user input:
Choice2 = input ("Please select the city Level two area list menu >>>:")
if Choice2 in Data[choice]:
While not Exit_flag:
For i3 in Data[choice][choice2]:
print ("\t\t" + i3)
choice3 = input ("Please select Level Three street >>>:")
if Choice3 in Data[choice][choice2]:
For I4 in Data[choice][choice2][choice3]:
print ("\t\t\t" + i4)
# When we finish printing the list, we don't want it to exit the program right away because I want it to make a choice .
# If I'm typing Q I'm quitting the program and I'm typing D, I'm going to go back to the parent menu
Choice4 = input ("The ultimate menu layer, whether to exit program >>>:")
if Choice4 = = "B":
Pass # Go directly to the parent menu
elif Choice4 = = "Q":
# Change the flag value, exit the program
Exit_flag = True
Else: # If you do not add this judgment, there will be a problem, because my choice walk is pass, to this layer, enter any character he will return to the upper menu, so in else add a check:
print ("The current directory is already the last level, you can choose whether to exit the program >>>: b/q")
While not Exit_flag:
Choice5 = input ("Please enter select >>>:")
if Choice5 = = "B":
Break
elif Choice5 = = "Q":
# Change the flag value, exit the program
Exit_flag = True
if choice3 = = "B":
Break # Go directly to the parent menu
elif Choice3 = = "Q":
Exit_flag = True
if Choice2 = = "B":
Break # Go directly to the parent menu
elif Choice2 = = "Q":
# Change the flag value, exit the program
Exit_flag = True



Python "Learning Path 03" collection series with multi-level menu Demo

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.