I have a list of NamedTuple
s (short extract below) and wrote some code to add the same random value to each of the a
and b
values in the tuple. The list will vary in length.
This works ok but I now need to add a different random value to each of the a
and b
values.
So, as written randomise
will generate a random value between -5 and 5 and apply that value to all instances of a
and b
. Now I want to add a different value to each instance of a
and b
.
What is the quickest way (in terms of execution) of doing this?
Extract:
list_one = [Test(a=820, b=625, time=1643282249.9990437), Test(a=820, b=624, time=1643282250.0470896), Test(a=820, b=623, time=1643282250.1034527), Pass(test_type='do1', pass='ffg3', time=1643282250.7834597)]
Code:
randomise = random.randint(-5, 5)
list_one = [Test(t.a + randomise, t.b + randomise, t.time) if isinstance(t, Test) else t for t in list_one]