I need to build a matrix with some values i have but as a tuple where i've a dictionary where the vales are coordinator and the keys are just an id, what i need to do is build a 5X6 matrix and append the values in My code is like this:
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)
unfortunately i'm getting this when running the code
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>]]
Anyone an idea how to get the values???
CodePudding user response:
Based on the limited description, I made the assumption you want to use the index of your dictionary as the indices for your matrix. If this is the case, it appears your dictionary does not have a 0 index, so in the list comprehension below, you'll want to start your ranges from 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)]]
CodePudding user response:
( x for x in ...) is understood as a generator.
is not necessary to explicit tuple(), 'cause your data are already tuple
[[DIC[element] for element in DIC for x in range(w)] for y in range(h)]
This do the job