0
リストのキーと数値をどのように取得し、それらをPythonを使用して独自の配列に配置するのですか?ベローは、これまでの私のコードです:私は、現在取得していますpythonを使用してリスト内のキーと値を区切りますか?
def make_chart(size, items): #this creates the grid
keys = list(items.keys())
chart = list()
row = []
for r in range(len(keys)):
for i in range(size):
row.append({"w":0, "v":0, "keys":[]})
chart.append(row)
return chart
def fill_chart(size, items, chart):
keys = list(items.keys())
values = list(items.values())
weight = []
#w, h = size +1, len(keys)
'''
here is where I need to separate the values for "w" and "v" and place them in their own arrays or lists that way I can fill the rest of the grid with zeros then try and run a Knapsack Problem on the information.
'''
def main():
iphone = {"w": 1, "v": 3000} #w is weight and v is value
guitar = {"w": 1, "v": 2000}
tablet = {"w": 2, "v": 3000}
dog = {"w":1, "v": 4000}
items = {"iphone":iphone, "guitar":guitar, "tablet":tablet, "dog":dog}
chart = make_chart(4, items)
chart = fill_chart(4, items, chart)
if __name__ == "__main__":
main()
([「W」:1、「v'3000])私は値をプリントアウトするが、私はW値またはV値にアクセスすることができたときに使用するためにそれらを独自の配列に配置します。私はstackoverflowや他のサイトで他のチュートリアルやその他の質問を参照しようとしましたが、何もこのスタイルに一致していないようです。 【行のアイテムのアイテム[ "W"]]
'all_w =' –
ので、キーと値を分離しようとしていますか? 'items'オブジェクトから? – Chinny84