我需要創建一個字典,其中的鍵是一個字串的元組,值是整數。我已經使用 CSV 模塊完成了這個任務,但是使用 pandas,我可以更好地管理我的資料。
基本上我想要這樣:
print(Supply)
{(1, 1) 。400, (1, 2)。0, (2, 1) 。1500, (2, 2) 。0, (3, 1) 。900, (3, 2) 。0}。
變成這樣:
print(Supply)
{('1', '1') 。400, ('1'/span>, '2'/span>): 0, ('2'/span>, '1'): 1500, ('2'/span>, '2'): 0, ('3'/span>, '1'): 900, ('3', '2') 。0}。
uj5u.com熱心網友回復:
這就完成了你想要的轉換:
Supply = {tuple(str(i) for i in key)。) val for key, val in Supply. items()}。
print(Supply)
# {('1', '1'): 400, ('1', '2'): 0, ('2', '1'): 1500, ('2', '2'): 0, ('3', '1'): 900, ('3', '2'): 0}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310227.html
標籤:
