即使字典外的相同变量发生变化,字典内的变量值也不会改变。为什么?
Variable inside dictionary value is unchanged even though same variable outside dictionary is changed. Why?
我有一个字典 restaurants
,我想根据用户输入的日期更改 restaurants
中嵌套列表中 opening hours
和 closing hours
的值.
opening_time=0
closing_time=0
##snippet of the dictionary
restaurants={"Macdonald's":\
\
[{"Monday":[700,2400],\
"Tuesday":[700,2400],\
"Wednesday":[700,2400],\
"Thursday":[700,2400],\
"Friday":[700,2400],\
"Saturday":[700,2400],\
"Sunday":[1000,2200]},\
\
"Block XXX, #01-XX",\
"Fast food restaurant known for its all round excellent menu.",\
\
["Breakfast",[opening_time,1100],\
{"Egg McMuffin":"",\
"Hotcakes":"",\
"Big Breakfast":""}],\
["Lunch/Dinner",[1100,closing_time],\
{"Double Cheeseburger":".20",\
"McChicken":".95",\
"Big Mac":".70",\
"Chicken McNuggets (6pcs)":".95",\
"McWings":".95"}],\
["All Day",[opening_time,closing_time],\
{"Fillet-O-Fish":".60",\
"Corn Cup":".95"}]]}
我希望代码循环并打印所有餐厅和菜单,同时指示所述餐厅和菜单是否在用户输入的时间内可用。
for key in restaurants: #key refers to restaurant name
print("","",sep='\n')
if day_now in restaurants.get(key)[0].keys(): #check if restaurant is open on that day
opening_time=restaurants.get(key)[0][day_now][0] #set opening and closing hours to those on that day
closing_time=restaurants.get(key)[0][day_now][1]
if time_now>=opening_time and time_now<closing_time: #check if restaurant is open within that time period
status="Open"
open_restaurants.update(restaurants)
print(key,"Status: "+status,"Opening Hours Today:"+str(opening_time)+" to "+str(closing_time),\
"Location: "+restaurants.get(key)[1],"Description: "+restaurants.get(key)[2],sep='\n')
for i in range(3, len(restaurants.get(key))): #goes through the menus the restaurant has
print(restaurants.get(key)[i][1][0]) #prints 0
print(restaurants.get(key)[i][1][1]) #prints 0
if time_now>=restaurants.get(key)[i][1][0] and time_now<restaurants.get(key)[i][1][1]: #check if menu have
print("")
print(restaurants.get(key)[i][0]+" Menu: Available","Item: Cost:",sep='\n')
for item in restaurants.get(key)[i][2].keys():
print(item, restaurants.get(key)[i][2][item],sep=' ')
else:
print("")
print(restaurants.get(key)[i][0]+" Menu: Unavailable","Item: Cost:", sep='\n')
for item in restaurants.get(key)[i][2].keys():
print(item, restaurants.get(key)[i][2][item],sep=' ')
else:
closed_restaurants.update(restaurants)
status="Closed"
print(key,"Status: "+status,"Opening Hours Today:"+str(opening_time)+" to "+str(closing_time),\
"Location: "+restaurants.get(key)[1],"Description: "+restaurants.get(key)[2], sep='\n')
else:
closed_restaurants.update(restaurants)
status="Closed"
print(key,"Status: "+status,"Opening Hours Today:"+str(opening_time)+" to "+str(closing_time),\
"Location: "+restaurants.get(key)[1],"Description: "+restaurants.get(key)[2], sep='\n')
print(opening_time) #prints the correct opening and closing hours
print(closing_time)
但是,字典中的 opening hours
和 closing hours
变量无法在循环中分配给所需的值,并保持在循环外首次分配的状态。
直接打印变量名,新值赋值成功。
有人可以帮我解决这里的问题吗?谢谢。
让我们用这个简单的例子来说明你的假设在哪里是错误的:
var = 1
lst = [var]
var = 2
print(lst)
这应该打印什么,[1]
或 [2]
?它将打印 [1]
。您在这里拥有的不是变量引用列表,而是整数列表。您获取了 var
的值,并将其放入列表中。一份,如果你想要的话。
这个呢?
a = 1
b = a
a = 2
同样,在此之后 b
仍然是 1
。你把写在 a
存储位置的内容,放在 b
存储位置。
你需要实际更新字典里面的值,更新opening_hours
.
是不够的
我有一个字典 restaurants
,我想根据用户输入的日期更改 restaurants
中嵌套列表中 opening hours
和 closing hours
的值.
opening_time=0
closing_time=0
##snippet of the dictionary
restaurants={"Macdonald's":\
\
[{"Monday":[700,2400],\
"Tuesday":[700,2400],\
"Wednesday":[700,2400],\
"Thursday":[700,2400],\
"Friday":[700,2400],\
"Saturday":[700,2400],\
"Sunday":[1000,2200]},\
\
"Block XXX, #01-XX",\
"Fast food restaurant known for its all round excellent menu.",\
\
["Breakfast",[opening_time,1100],\
{"Egg McMuffin":"",\
"Hotcakes":"",\
"Big Breakfast":""}],\
["Lunch/Dinner",[1100,closing_time],\
{"Double Cheeseburger":".20",\
"McChicken":".95",\
"Big Mac":".70",\
"Chicken McNuggets (6pcs)":".95",\
"McWings":".95"}],\
["All Day",[opening_time,closing_time],\
{"Fillet-O-Fish":".60",\
"Corn Cup":".95"}]]}
我希望代码循环并打印所有餐厅和菜单,同时指示所述餐厅和菜单是否在用户输入的时间内可用。
for key in restaurants: #key refers to restaurant name
print("","",sep='\n')
if day_now in restaurants.get(key)[0].keys(): #check if restaurant is open on that day
opening_time=restaurants.get(key)[0][day_now][0] #set opening and closing hours to those on that day
closing_time=restaurants.get(key)[0][day_now][1]
if time_now>=opening_time and time_now<closing_time: #check if restaurant is open within that time period
status="Open"
open_restaurants.update(restaurants)
print(key,"Status: "+status,"Opening Hours Today:"+str(opening_time)+" to "+str(closing_time),\
"Location: "+restaurants.get(key)[1],"Description: "+restaurants.get(key)[2],sep='\n')
for i in range(3, len(restaurants.get(key))): #goes through the menus the restaurant has
print(restaurants.get(key)[i][1][0]) #prints 0
print(restaurants.get(key)[i][1][1]) #prints 0
if time_now>=restaurants.get(key)[i][1][0] and time_now<restaurants.get(key)[i][1][1]: #check if menu have
print("")
print(restaurants.get(key)[i][0]+" Menu: Available","Item: Cost:",sep='\n')
for item in restaurants.get(key)[i][2].keys():
print(item, restaurants.get(key)[i][2][item],sep=' ')
else:
print("")
print(restaurants.get(key)[i][0]+" Menu: Unavailable","Item: Cost:", sep='\n')
for item in restaurants.get(key)[i][2].keys():
print(item, restaurants.get(key)[i][2][item],sep=' ')
else:
closed_restaurants.update(restaurants)
status="Closed"
print(key,"Status: "+status,"Opening Hours Today:"+str(opening_time)+" to "+str(closing_time),\
"Location: "+restaurants.get(key)[1],"Description: "+restaurants.get(key)[2], sep='\n')
else:
closed_restaurants.update(restaurants)
status="Closed"
print(key,"Status: "+status,"Opening Hours Today:"+str(opening_time)+" to "+str(closing_time),\
"Location: "+restaurants.get(key)[1],"Description: "+restaurants.get(key)[2], sep='\n')
print(opening_time) #prints the correct opening and closing hours
print(closing_time)
但是,字典中的 opening hours
和 closing hours
变量无法在循环中分配给所需的值,并保持在循环外首次分配的状态。
直接打印变量名,新值赋值成功。
有人可以帮我解决这里的问题吗?谢谢。
让我们用这个简单的例子来说明你的假设在哪里是错误的:
var = 1
lst = [var]
var = 2
print(lst)
这应该打印什么,[1]
或 [2]
?它将打印 [1]
。您在这里拥有的不是变量引用列表,而是整数列表。您获取了 var
的值,并将其放入列表中。一份,如果你想要的话。
这个呢?
a = 1
b = a
a = 2
同样,在此之后 b
仍然是 1
。你把写在 a
存储位置的内容,放在 b
存储位置。
你需要实际更新字典里面的值,更新opening_hours
.