我需要用我有的一些值構建一個矩陣,但作為一個元組,我有一個字典,其中 vales 是協調器,鍵只是一個 id,我需要做的是構建一個 5X6 矩陣并將值附加到我的代碼是這樣的:
DIC = {1: (0.0, 0.0), 6: (0.0, 0.14), 2: (0.14, 0.0), 7: (0.14, 0.14), 3: (0.28, 0.0), 8: (0.28, 0.14), 4: (0.42, 0.0), 9: (0.42, 0.14), 5: (0.56, 0.0), 10: (0.56, 0.14), 11: (0.0, 0.28), 12: (0.14, 0.28), 13: (0.28, 0.28), 14: (0.42, 0.28), 15: (0.56, 0.28), 16: (0.0, 0.42), 17: (0.14, 0.42), 18: (0.28, 0.42), 19: (0.42, 0.42), 20: (0.56, 0.42), 21: (0.0, 0.56), 22: (0.14, 0.56), 23: (0.28, 0.56), 24: (0.42, 0.56), 25: (0.56, 0.56), 26: (0.0, 0.7), 27: (0.14, 0.7), 28: (0.28, 0.7), 29: (0.42, 0.7), 30: (0.56, 0.71)}
w, h = 5, 6
position = [[(tuple(DIC[element]) for element in DIC) for x in range(w)] for y in range(h)]
print(position)
不幸的是我在運行代碼時得到了這個
at 0x7fccc7ff37b0>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3820>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3890>], [<generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3900>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3970>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff39e0>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3a50>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3ac0>], [<generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3b30>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3ba0>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3c10>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3c80>, <generator object <listcomp>.<listcomp>.<genexpr> at 0x7fccc7ff3cf0>]]
有人知道如何獲取值嗎???
uj5u.com熱心網友回復:
基于有限的描述,我假設您想使用字典的索引作為矩陣的索引。如果是這種情況,您的字典似乎沒有 0 索引,因此在下面的串列理解中,您需要從 1 開始您的范圍。
position = [ [ DIC[x*y] for x in range(1, w 1) ] for y in range(1, h 1) ]
position
[[(0.0, 0.0), (0.14, 0.0), (0.28, 0.0), (0.42, 0.0), (0.56, 0.0)], [(0.14, 0.0), (0.42, 0.0), (0.0, 0.14), (0.28, 0.14), (0.56, 0.14)], [(0.28, 0.0), (0.0, 0.14), (0.42, 0.14), (0.14, 0.28), (0.56, 0.28)], [(0.42, 0.0), (0.28, 0.14), (0.14, 0.28), (0.0, 0.42), (0.56, 0.42)], [(0.56, 0.0), (0.56, 0.14), (0.56, 0.28), (0.56, 0.42), (0.56, 0.56)], [(0.0, 0.14), (0.14, 0.28), (0.28, 0.42), (0.42, 0.56), (0.56, 0.71)]]
uj5u.com熱心網友回復:
( x for x in ...) 被理解為一個生成器。
沒有必要顯式 tuple(),因為你的資料已經是元組
[[DIC[element] for element in DIC for x in range(w)] for y in range(h)]
這做的作業
uj5u.com熱心網友回復:
您得到的錯誤是由于在內部理解中使用括號而不是括號引起的:
(tuple(DIC[element]) for element in DIC) 是一個迭代器。
[tuple(DIC[element]) for element in DIC] 是一個串列。
請注意,獲取密鑰然后重新訪問字典是不必要的。將值轉換為元組(它們已經是元組)也是如此。list(DIC.values())將產生與上述串列理解相同的結果。
除此之外,您要實作的目標尚不清楚。您似乎有元組字典,其鍵對應于 5x6 矩陣中的基于 1 的位置。如果是這種情況,您可能會得到這樣的預期結果:
position = [[DIC[r c 1] for c in range(w)] for r in range(0,w*h,w) ]
print(position)
[[(0.0, 0.0), (0.14, 0.0), (0.28, 0.0), (0.42, 0.0), (0.56, 0.0)],
[(0.0, 0.14), (0.14, 0.14), (0.28, 0.14), (0.42, 0.14), (0.56, 0.14)],
[(0.0, 0.28), (0.14, 0.28), (0.28, 0.28), (0.42, 0.28), (0.56, 0.28)],
[(0.0, 0.42), (0.14, 0.42), (0.28, 0.42), (0.42, 0.42), (0.56, 0.42)],
[(0.0, 0.56), (0.14, 0.56), (0.28, 0.56), (0.42, 0.56), (0.56, 0.56)],
[(0.0, 0.7), (0.14, 0.7), (0.28, 0.7), (0.42, 0.7), (0.56, 0.71)]]
如果字典中的鍵不是基于坐標,而您只想按順序分配值,則可以在 DIC.values() 上使用迭代器:
iDIC = iter(DIC.values())
position = [[next(iDIC) for _ in range(w)] for _ in range(h) ]
這給出了相同的結果,但不依賴于鍵和位置之間的任何關系(它只取決于字典中的順序)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/375703.html
下一篇:合并多個字典以獲取自定義物件陣列
