我正在学习课程中的背包问题:https://courses.edx.org/courses/course-v1:MITx+6.00.2x+3T2019/course/
具有以下输入:
cows = {"Jesse": 6, "Maybel": 3, "Callie": 2, "Maggie": 5}
我必须返回一个列表作为输出,比如:
[['Jesse', 'Maybel'], ['Callie', 'Maggie']]
我编写的函数似乎运行良好,因为它返回正确的结果:
def greedy_cow_transport(cows,capacity):
ocupied = 0
current_trip = []
all_trips = []
while(True):
for cow in cows:
if ocupied+cow[1] < capacity:
current_trip.append((cow[0]))
ocupied += cow[1]
cows.remove(cow)
else:
all_trips.append(current_trip)
ocupied = 0
current_trip = []
if len(cows)==0:
all_trips.append(current_trip)
break
return all_trips
问题是在打过一次电话之后:
order = copy.copy(sorted(cows.items(), key=lambda x: x[1],reverse= True ))
greedy_cow_transport(order,capacity)
变量顺序是一个空列表。 我不明白为什么会发生这种情况,因为我只在函数的作用域内使用.remove方法。为什么这些会影响我的主要变量?你知道吗
既然您试图使用
copy.copy
,我假设您理解python通过参数的值传递对对象的引用。您只需要保留对排序顺序的引用。你知道吗脚本
输出
注意,如果有嵌套列表,则需要
deepcopy
。你知道吗考虑这个例子:
您可能期望参数传递给函数的行为类似于整数:
您可能认为Python总是复制参数以在函数中使用,但事实并非如此。执行
i = 6
操作时,将变量名i
设置为引用值为6
的新对象。因此,不会修改全局i
。你知道吗但是,当您使用
list.remove(x)
时,您正在修改作为参数传递的实际对象。名为list
的变量仍然引用原始对象。你知道吗当然,这个解释可能会让人很困惑。添加一些
id()
将有助于澄清问题。你知道吗现在使用整数:
相关问题 更多 >
编程相关推荐