Case #1: one dimension tuple
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"
Case #2: Two dimension tuple
Example: DICT_ROLES[961] # will show 'Back-End Programmer'
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
command, which will show you the fields for any object... that would have shown the_asdict
function directly. – Vernedict
instead of 'namedtuple', and pass the namedtuple into the initializer. Remember that if you're used to Cxx,class Town(x)
isn't the constructor,def __init__(self, *args, **kwargs)
inside it is. – Vernedir
is just a python built-in... you can run it on any python object, in a console or in a script (where it returns a list you can print or do whatever with), and it will return a list of (nearly) all the attributes the object. helpful if you're trying to figure out how an unknown object works. – Verne