如何在python中访问嵌套字典键的值?
我有嵌套的字典,我想更新第二个字典“key”值的值,这样它也应该反映字典值如何在python中访问嵌套字典键的值?,python,class,dictionary,Python,Class,Dictionary,我有嵌套的字典,我想更新第二个字典“key”值的值,这样它也应该反映字典值 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
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},}
我想更新以下键的值
self.seats['screen1','day4','show4'] =90
以便:
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},}
如何在python中实现这一点
编辑:
输出:
2
seat booked
90
seat booked
第二次应该打印“对不起,第一屏没有座位”。请尝试其他屏幕的
帮助我识别代码中的问题?您的字典有两个嵌套级别,一个使用“screenX”、“dayX”元组索引,另一个使用showX字符串索引。遵守以下规定:
>>> foo.seats['screen1', 'day4']
{'show4': 90, 'show2': 100, 'show1': 100, 'show3': 100}
>>> foo.seats['screen1', 'day4']['show4']
90
第一个表达式提供了一个字典,为了得到所需的元素,您必须再次索引该字典。所以最后的表达是:
foo.seats['screen1', 'day4']['show4']
# ^ ^
# | |
# +-- First level +-- Second level
使用以下命令:
self.seats[('screen1', 'day4')]['show4'] = 90
输出:
首先访问screen1 day4,通过'screen1','day4'元组作为self.seats字典中的键显示。然后通过“show4”访问内部字典的“show4”键,并将其值设置为90
看起来您对python还不熟悉。让我们试着通过以下三个步骤来理解思维过程
步骤1:
“screen1”,“day4”提供对{'show1':100,'show2':100,'show3':100,'show4':100}内部字典的访问
self.seats[('screen1', 'day4')]
{'show1': 100, 'show2': 100, 'show3': 100, 'show4': 100}
步骤2:
现在,通过上一步获得的字典中的“show4”键访问show4
self.seats[('screen1', 'day4')]['show4']
100
步骤3:
将screen1-day4-show4获得的值更新为90
self.seats[('screen1', 'day4')]['show4'] = 90
self.seats[('screen1', 'day4')]['show4']
90
代码解决方案:
检查这种方法是否适合您
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 are valid and 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 and the 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 the 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')
self.seats['screen1','day4']['show4']=90类值中没有更新新值。上面的代码中没有screen7。此外,请按照大写字母符号命名类@RahulGupta更新了类。您之所以获得此输出,是因为您再次使用screen_seat类中定义的座位值创建screen_预订的新实例。嘿,Lucifier!我已经为你的问题编写了解决方案。看看它是否适合你。
self.seats[('screen1', 'day4')]['show4'] = 90
self.seats[('screen1', 'day4')]['show4']
90
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 are valid and 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 and the 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 the 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')