案例1:一维元组
TUPLE_ROLES = (
(912,"Role 21"),
(913,"Role 22"),
(925,"Role 23"),
(918,"Role 24"),
)
TUPLE_ROLES[912] #==> Error because it is out of bounce.
TUPLE_ROLES[ 2] #==> will show Role 23.
DICT1_ROLE = {k:v for k, v in TUPLE_ROLES }
DICT1_ROLE[925] # will display "Role 23"
情况2:二维元组
示例:DICT_ROLES [961]#将显示“后端编程器”
NAMEDTUPLE_ROLES = (
('Company', (
( 111, 'Owner/CEO/President'),
( 113, 'Manager'),
( 115, 'Receptionist'),
( 117, 'Marketer'),
( 119, 'Sales Person'),
( 121, 'Accountant'),
( 123, 'Director'),
( 125, 'Vice President'),
( 127, 'HR Specialist'),
( 141, 'System Operator'),
)),
('Restaurant', (
( 211, 'Chef'),
( 212, 'Waiter/Waitress'),
)),
('Oil Collector', (
( 211, 'Truck Driver'),
( 213, 'Tank Installer'),
( 217, 'Welder'),
( 218, 'In-house Handler'),
( 219, 'Dispatcher'),
)),
('Information Technology', (
( 912, 'Server Administrator'),
( 914, 'Graphic Designer'),
( 916, 'Project Manager'),
( 918, 'Consultant'),
( 921, 'Business Logic Analyzer'),
( 923, 'Data Model Designer'),
( 951, 'Programmer'),
( 953, 'WEB Front-End Programmer'),
( 955, 'Android Programmer'),
( 957, 'iOS Programmer'),
( 961, 'Back-End Programmer'),
( 962, 'Fullstack Programmer'),
( 971, 'System Architect'),
)),
)
#Thus, we need dictionary/set
T4 = {}
def main():
for k, v in NAMEDTUPLE_ROLES:
for k1, v1 in v:
T4.update ( {k1:v1} )
print (T4[961]) # will display 'Back-End Programmer'
# print (T4) # will display all list of dictionary
main()
dir
命令,它将为您显示任何对象的字段..._asdict
直接显示该功能。