Issue
Provided the object array:
textures = [
{
"id": 1,
"name": "cement",
"apply_to": ["wall", "basement"],
},
{
"id": 2,
"name": "fabric",
"apply_to": ["window"],
},
{
"id": 3,
"name": "brick",
"apply_to": ["wall"],
},
...
...
...
]
Suppose I wanted to pick a random object out of that array with the keyword 'wall' in that apply_to property?
So that I get either id (1,3) returned at random?
How can I use 'random.choice' or another random function to do so?
Solution
wall_textures = [texture for texture in textures if 'wall' in texture['apply_to']]
random_wall_texture = random.choice(wall_textures)
Agree that not the best way but this will let you make the choice randomly
Answered By - Arunbh Yashaswi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.