Imagine that you have a list of numbers and you would like to find the index of numbers meeting a certain condition (say > 3). We would then like to have the smallest index (meeting the above condition) being a multiple of a certain number (here 4). Below might be a good start. What would you suggest for the rest of the code?
a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16]
b = 4
output = [i for i, x in enumerate(a) if x > 3]
print(output) '''
CodePudding user response:
If you want to find the minumim index, you can use the min()
function:
a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16]
b = 4
output = [i for i, x in enumerate(a) if x > 3]
print(output, min(output))
CodePudding user response:
The indices are already obtained by the code given above. All you need to do is find the smallest index divisible by b
. You can achieve it as follows:
a = [1, 2, 3, 1, 2, 3, 5, 8, 9, 10 ,12, 12, 15, 16]
b = 4
# outputGT3 = output of all numbers > 3
outputGT3 = [i for i, x in enumerate(a) if x > 3]
print(outputGT3)
# outputDBb = output of all numbers divisible by b
outputDBb = [x for i, x in enumerate(outputGT3) if x % b==0]
print(outputDBb[0])
# since outputDBb is already sorted, the 1st element is the smallest.
CodePudding user response:
This question is unrelated to lists:
a, b = 3, 4
n = a b/2
result = n - (n%b)