Reputation: 93
I need to generate a x
and y
value based on width and height. Here, i have a list of dicts which looks like this,
myInput = [{"height":3,"value":5},{"height":5,"value":1} and so on..]
I am iterating over this dict to generate x and y values. Based on width, i want to generate x and based on height, i want to generate y. I haven't used width in code. The usage of width is to calculate x value. Here width is 4, so maximum x value is 4. I don't know how to make it in code. Help me with some solutions.
Here's the sample code,
width = 4
myInput = [{"height":3,"value":5},{"height":5,"value":1}]
temp_d = {}
result = []
for i in myInput:
print(i["height"])
for j in range(i["height"]):
temp_d["x"] = j
temp_d["y"] = j
temp_d["value"] = i["value"]
result.append(temp_d)
temp_d={}
print(result)
My Output:
[{'x': 0, 'y': 0, 'value': 5},
{'x': 1, 'y': 1, 'value': 5},
{'x': 2, 'y': 2, 'value': 5},
{'x': 0, 'y': 0, 'value': 1},
{'x': 1, 'y': 1, 'value': 1},
{'x': 2, 'y': 2, 'value': 1},
{'x': 3, 'y': 3, 'value': 1},
{'x': 4, 'y': 4, 'value': 1}]
Required Output:
[
{'x': 0, 'y': 0, 'value': 5}, {'x': 0, 'y': 1, 'value': 5},
{'x': 0, 'y': 2, 'value': 5}, {'x': 0, 'y': 3, 'value': 5},
{'x': 1, 'y': 0, 'value': 5}, {'x': 1, 'y': 1, 'value': 5},
{'x': 1, 'y': 2, 'value': 5}, {'x': 1, 'y': 3, 'value': 5},
{'x': 2, 'y': 0, 'value': 5}, {'x': 2, 'y': 1, 'value': 5},
{'x': 2, 'y': 2, 'value': 5}, {'x': 2, 'y': 3, 'value': 5},
{'x': 3, 'y': 0, 'value': 5}, {'x': 3, 'y': 1, 'value': 5},
{'x': 3, 'y': 2, 'value': 5}, {'x': 3, 'y': 3, 'value': 5},
{'x': 4, 'y': 0, 'value': 5}, {'x': 4, 'y': 1, 'value': 5},
{'x': 4, 'y': 2, 'value': 5}, {'x': 4, 'y': 3, 'value': 5},
{'x': 0, 'y': 4, 'value': 1}, {'x': 0, 'y': 5, 'value': 1},
{'x': 1, 'y': 4, 'value': 1}, {'x': 1, 'y': 5, 'value': 1},
{'x': 2, 'y': 4, 'value': 1}, {'x': 2, 'y': 5, 'value': 1},
{'x': 3, 'y': 4, 'value': 1}, {'x': 3, 'y': 5, 'value': 1},
{'x': 4, 'y': 4, 'value': 1}, {'x': 4, 'y': 5, 'value': 1},
]
Upvotes: 0
Views: 694
Reputation: 41
Since you want list to contain all three things width height and value, you have to loop over all of these to get the correct result, since you ignored the width in your code it is generating values using only height for both x and y, but you need different values for x and y, which I have used as k for width and j for height. Also in your expected output the height is not being reset to 0 for each dictionary element in myInput, it is set to previous height value + 1, so i added a curr variable to keep track of this height.
def my_func():
width = 4
myInput = [{"height":3,"value":5},{"height":5,"value":1}]
temp_d = {}
result = []
curr = 0
for i in myInput:
print(i["height"])
for k in range(width+1):
for j in range(curr,i["height"]+1):
temp_d["x"] = k
temp_d["y"] = j
temp_d["value"] = i["value"]
result.append(temp_d)
temp_d={}
curr = i["height"] + 1
print(result
Upvotes: 1
Reputation: 43
The output looks different for both inputs : {"height":3,"value":5},{"height":5,"value":1}
For {"height":3,"value":5}, it looks like the result is calculated after iterating over width and height
To achieve this, you can try below:
import itertools
for i in myInput:
for p,q in itertools.product(range(width+1),range(i["height"]+1)):
temp_d["x"] = p
temp_d["y"] = q
temp_d["value"] = i["value"]
result.append(temp_d)
temp_d={}
print(result)
But the output for {"height":5,"value":1} is different from output of {"height":3,"value":5} and the logic mentioned above will generate a different output. Could you elaborate more on the question so we can modify the logic appropriately?
Upvotes: 0