该项目是使用特定算法将项目分类到盒子中。我无法使用字典将项目作为值放入给定的框中。我的主要问题是,当字典中有多个值时,我无法弄清楚如何检索字典中键的 1 个值。我的第二个问题是我担心我的程序过于复杂并创建了不必要的功能。
我在使用此功能时遇到问题,尤其是:
def roomiest(boxList, itemList, boxDict, itemDict):
"""
For each item find the box with the greatest remaining allowed weight that can support the item and place the item in that box
:param boxList: The sorted list of boxes( large to small )
:param itemList: The sorted list of items ( large to small )
:param boxDict: Dict w/ boxes
:param itemDict: Dict w/ items
:return: If boxes were able to fit all items(1); items in box with individual weights(2); Box name with max
weight(3); items with their weights that were left behind(4)
"""
sortedItems = sortLargest2Small(itemList)
sortedBoxes = sortLargest2Small(boxList)
for item in sortedItems:
for box in sortedBoxes:
itemWeight = keywordSearchDict(item, itemDict)
boxRemWeight = keywordSearchDict(box, boxDict)
if itemWeight <= boxRemWeight:
itemDict[
box] = # Need to add item to box with its weight as well as modify the remaining weight the box
# can hold
对于上下文,这是我的代码。
这是文本文件的示例:pastebin