0
我能夠成功地將SQLITE數據庫中的值存儲在Python字典中。但是,試圖處理這些值的時候,我收到以下錯誤:使用SQLITE3和字典時的Python ValueError
'Traceback (most recent call last):
File "transportation problem.py", line 20, in <module>
I = set([i for (i,k) in d])
ValueError: too many values to unpack
這是否表明我的源數據庫出了問題,或者是有缺陷的我的代碼?
import sqlite3 as lite
import sys
from gurobipy import *
con = lite.connect('transport.sqlite')
produce = {1:[2,4], 2:[1,2,3], 3:[2,3,4]}
d = {}
with con:
cur = con.cursor()
cur.execute('SELECT idcustomer,idfactory,distance FROM distance')
result = cur.fetchall()
for idcustomer, idfactory, distance in result:
d["({},{})".format(idcustomer, idfactory)] = distance
I = set([i for (i,k) in d])
K = set([k for (i,k) in d])
J,M = multidict({1:3000, 2:3000, 3:3000})
任何幫助,高度讚賞!
編輯:
由安迪·海登提供的解決方案在第一次工作得很好,但是當我實現它變成我的代碼,我開始收到以下錯誤:
c[i,j,k] = cost[i,j] * weight[k]
KeyError: ('1,1', 1)
我張貼最初的代碼繼續如下:
produce = {1:[2,4], 2:[1,2,3], 3:[2,3,4]}
K, weight = multidict({1:5, 2:2, 3:3, 4:4})
cost = {(1,1):4, (1,2):6, (1,3):9,
(2,1):5, (2,2):4, (2,3):7,
(3,1):6, (3,2):3, (3,3):4,
(4,1):8, (4,2):5, (4,3):3,
(5,1):10, (5,2):8, (5,3):4,
}
c = {}
for i in I:
for j in J:
for k in produce[j]:
c[i,j,k] = cost[i,j] * weight[k]
我是否需要在循環的項目中添加'.iterititems'? 當我手動輸入SQLITE數據並將'd.iteritems'恢復爲'd'時,代碼可以毫無問題地工作。
任何建議將再次受到歡迎!