class - how to access values of nested dictionary keys in python? -


i have nested dictionaries, , want update values of second dictionaries' keys' value such should reflect dictionary value also.

class screen_seat:    def __init__(self,screen,show,num_seats,day):       self.screen_id = screen       self.show = show       self.num_seats = num_seats       self.seats = {('screen1','day4'):{'show1':100,'show2':100,'show3':100,'show4':100},                    ('screen1','day5'):{'show1':100,'show2':100,'show3':100,'show4':100},                    ('screen1','day6'):{'show1':100,'show2':100,'show3':100,'show4':100},                    ('screen1','day7'):{'show1':100,'show2':100,'show3':100,'show4':100},} 

i update value of below keys

self.seats['screen1','day4','show4'] =90 

so that:

self.seats = {('screen1','day4'):{'show1':100,'show2':100,'show3':100,'show4':**90**},                        ('screen1','day5'):{'show1':100,'show2':100,'show3':100,'show4':100},                        ('screen1','day6'):{'show1':100,'show2':100,'show3':100,'show4':100},                        ('screen1','day7'):{'show1':100,'show2':100,'show3':100,'show4':100},} 

how can in python?

edit:

class screen_seat:    def __init__(self,screen,show,num_seats,day):       self.screen_id = screen       self.show = show       self.num_seats = num_seats        self.seats = {('screen1','day4'):{'show1':100,'show2':100,'show3':100,'show4':100},       ('screen1','day5'):{'show1':100,'show2':100,'show3':100,'show4':100},       ('screen1','day6'):{'show1':100,'show2':100,'show3':100,'show4':100},       ('screen1','day7'):{'show1':100,'show2':100,'show3':100,'show4':100},           ('screen2','day1'):{'show1':100,'show2':100,'show3':100,'show4':100},       ('screen2','day2'):{'show1':100,'show2':100,'show3':100,'show4':100},       ('screen2','day3'):{'show1':100,'show2':100,'show3':100,'show4':100},                      }  class screen_booking(screen_seat):    def __init__(self,screen,show,num_seats,day):        screen_seat.__init__(self,screen,show,num_seats,day)        self.booking_screen = screen        self.booking_show = show        self.booking_day=day        self.booking_seats=num_seats     def checkavailability(self):        self.seats[self.booking_screen,self.booking_day][self.booking_show]        if (self.seats[self.booking_screen,self.booking_day][self.booking_show] > int(self.booking_seats)):           self.seats[self.booking_screen,self.booking_day][self.booking_show]=(self.seats[self.booking_screen,self.booking_day][self.booking_show]-int(self.booking_seats))            #print self.seats[self.booking_screen,self.booking_day][self.booking_show]           print 'seat booked'        else:            print 'sorry, no seats available in screen1. please try other screens'   a1 = screen_booking('screen1','show1','98','day4') a1.checkavailability() a1 = screen_booking('screen1','show1','10','day4') 

output:

2 seat booked 90 seat booked 

second time should print 'sorry, no seats available in screen1. please try other screens'

help me identify issue in code?

use this:

self.seats[('screen1', 'day4')]['show4'] = 90 

output:

self.seats {('screen1', 'day4'): {'show1': 100, 'show2': 100, 'show3': 100, 'show4': 90},  ('screen1', 'day5'): {'show1': 100, 'show2': 100, 'show3': 100, 'show4': 100},  ('screen1', 'day6'): {'show1': 100, 'show2': 100, 'show3': 100, 'show4': 100},  ('screen1', 'day7'): {'show1': 100, 'show2': 100, 'show3': 100, 'show4': 100}} 

first access screen1 day4 shows ('screen1', 'day4') tuple key in self.seats dictionary. access 'show4' key of inner dictionary 'show4' , set value 90.

it seems new python. let try understand thought process following 3 steps.

step1:

('screen1', 'day4') gives access {'show1': 100, 'show2': 100, 'show3': 100, 'show4': 100} inner dictionary.

self.seats[('screen1', 'day4')] {'show1': 100, 'show2': 100, 'show3': 100, 'show4': 100} 

step2:

now access show4 'show4' key in obtained dictionary previous step.

self.seats[('screen1', 'day4')]['show4'] 100 

step3:

update value obtained screen1-day4-show4 90.

self.seats[('screen1', 'day4')]['show4'] = 90  self.seats[('screen1', 'day4')]['show4'] 90 

code solution:

check approach if works you.

class screenbooking(object):      def __init__(self):         super(screenbooking, self).__init__()         self.seats = {             ('screen1','day4'):{'show1':100,'show2':100,'show3':100,'show4':100},             ('screen1','day5'):{'show1':100,'show2':100,'show3':100,'show4':100},             ('screen1','day6'):{'show1':100,'show2':100,'show3':100,'show4':100},             ('screen1','day7'):{'show1':100,'show2':100,'show3':100,'show4':100},             ('screen2','day1'):{'show1':100,'show2':100,'show3':100,'show4':100},             ('screen2','day2'):{'show1':100,'show2':100,'show3':100,'show4':100},             ('screen2','day3'):{'show1':100,'show2':100,'show3':100,'show4':100},         }         self.shows = ['show1', 'show2', 'show3', 'show4']       def check_valid_details(self, screen, show, day):         """         check if booking details valid , return true/false accordingly.         """         if (screen, day) not in self.seats or show not in self.shows:             return  false         return true      def book_seats(self, screen, show, no_of_seats, day):         """         book seats after checking valid booking details , remaining seats.         """         valid_details = self.check_valid_details(screen, show, day)         if not valid_details:             print 'invalid booking details!'             return         show_total_seats = self.seats[(screen, day)][show]         if show_total_seats > int(no_of_seats):             show_remaining_seats = show_total_seats - int(no_of_seats)             self.seats[(screen, day)][show] = show_remaining_seats #update seats count             print '%s seat(s) booked'%(no_of_seats)         else:             print 'sorry, no seats available in %s. please try other screens'%(screen)  a1 = screenbooking() a1.book_seats('screen1','show1','98','day4') a1.book_seats('screen1','show1','10','day4') 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -