[英]how to access values of nested dictionary keys in python?
我有嵌套的字典,我想更新第二个字典键值的值,这样它也应该反映字典值。
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中做到这一点?
编辑:
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')
输出:
2
seat booked
90
seat booked
第二次它应该打印“抱歉,Screen1 中没有可用座位。 请尝试其他屏幕'
帮我找出代码中的问题?
您的字典有两个嵌套级别,一个用('screenX', 'dayX')
元组索引,另一个用showX
字符串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
输出:
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}}
首先访问由('screen1', 'day4')
元组显示的 screen1 day4 作为self.seats
字典中的键。 然后访问'show4'
内的字典通过键'show4'
,并将其值设定为90。
看来你是python的新手。 让我们尝试通过以下 3 个步骤来理解思维过程。
第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
第三步:
将获得的 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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.