即使更改了字典外的相同变量,字典内的变量值也不会更改。为什么?

我有一个字典restaurants,并且想根据用户输入的日期来更改opening hours中嵌套列表中closing hoursrestaurants的值。

opening_time=0
closing_time=0

##snippet of the dictionary 
restaurants={"Macdonald's":\
             \
             [{"Monday":[700,2400],\
               "Tuesday":[700,\
               "Wednesday":[700,\
               "Thursday":[700,\
               "Friday":[700,\
               "Saturday":[700,\
               "Sunday":[1000,2200]},\
              \
              "Block XXX,#01-XX",\
              "Fast food restaurant known for its all round excellent menu.",\
              \
              ["Breakfast",[opening_time,1100],\
               {"Egg McMuffin":"$1",\
                "Hotcakes":"$1",\
                "Big Breakfast":"$1"}],\
              ["Lunch/Dinner",[1100,closing_time],\
               {"Double Cheeseburger":"$3.20",\
                "McChicken":"$3.95",\
                "Big Mac":"$4.70",\
                "Chicken McNuggets (6pcs)":"$4.95",\
                "McWings":"$4.95"}],\
              ["All Day",\
               {"Fillet-O-Fish":"$4.60",\
                "Corn Cup":"$1.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",sep=' ')            
        else:
            closed_restaurants.update(restaurants)
            status="Closed"
            print(key,sep='\n')
    else:
        closed_restaurants.update(restaurants)
        status="Closed"
        print(key,sep='\n')

print(opening_time) #prints the correct opening and closing hours
print(closing_time) 

但是,字典中的opening hoursclosing hours变量无法在循环中分配给所需的值,并保持不变,因为它们是第一次在循环外部分配的。

直接打印变量名表明新值已成功分配。

有人可以在这里帮助我解决这个问题吗?谢谢。

qmm1030798770 回答:即使更改了字典外的相同变量,字典内的变量值也不会更改。为什么?

让我们使用这个简单的例子来弄清楚您的假设在哪里错误:

var = 1
lst = [var]
var = 2
print(lst)

[1][2]应该打印什么?它将打印[1]。您这里没有的不是变量引用列表,而是整数列表。您采用了var的值,并将其放入列表中。副本,如果需要的话。

那呢?

a = 1
b = a
a = 2

同样,此后b仍然是1。您将写入的内容存储在a的位置,并将其放置在存储b的位置。

您实际上需要更新字典中的值,而仅更新opening_hours是不够的。

本文链接:https://www.f2er.com/3124205.html

大家都在问