I think the downvotes are justified because you did not provide any code at all to show us what you tried so far, so that we know where you are stuck with your code. Also i think the way you wrote the requirements is a bit confusing (0eth probably means nth sometimes?). Anyways, this is not hard to do – All you need is zip
, min
, a list comprehension and random.choice
and you can work with n
lists:
from random import choice
a = [100, 101, 100, 99, 100, 110]
b = [100, 102, 101, 98, 101, 130]
c = [100, 103, 100, 99, 100, 120]
# In order to work properly with indexes
# we should rely on a list of lists
lists = [a, b, c]
# Collect the last value of each sublist in lists
last_values = [sublist[-1] for sublist in lists]
# Your d list
result = []
# zip combines each element from each given list on the same index
# splat the sublists with * (keeps the proper order of sublists)
for zipped in zip(*lists):
# min selects the lowest value in the zipped values
lowest = min(zipped)
# find all occurences of the lowest value within zipped values
indexes = [i for i, v in enumerate(zipped) if v == lowest]
# pick a random index (len(indexes) doesn't matter)
random_index = choice(indexes)
# Divide the last value in the picked list with the lowest value
value = last_values[random_index] / lowest
# And add to result
result.append(value)
print(result)