The problem is for all the numbers (1 - 20) the highest single digit (1 - 9) of any of the numbers is divisible by.
I have a for loop as given below:
values = [[] for value in range(1, 11)]
for num in range(1, 21):
highest = 0
for div in range(1, 10):
if(num % div == 0 and div > highest):
highest = div
values[highest].append(num)
The following for loop output:
[[], [1, 11, 13, 17, 19], [2], [3], [4], [5, 10, 15, 20], [6, 12], [7, 14], [8, 16], [9, 18]]
The empty list [] that in the output can be ignored. For example:
[[1, 11, 13, 17, 19], [2], [3], [4], [5, 10, 15, 20], [6, 12], [7, 14], [8, 16], [9, 18]]
I want to convert the following for loop to list comprehension, can anyone please help me.
highest) and then "flipping" it?nums = list(range(1, 21)) ; values = [[nums.pop(i) for i in range(len(nums) - 1, -1, -1) if not nums[i] % d] for d in range(9, 0, -1)]. But it will generate list in reversed order.reversed(and dorange(9, -1, -1)to not skip index 0) and it will be what OP needs. Post it as an answer! | Now excuse me, I need to take my time to understand how you did that because it's amazing! :Drange(9, -1, -1)is very bad idea, you can say why.