-
-
Save bbookman/c5cf0516e9fcf0c1fdbdcff7cd43867a to your computer and use it in GitHub Desktop.
''' | |
Use a nested list comprehension to find all of the numbers from 1-100 that are divisible by any single digit besides 1 (2-9) | |
''' | |
#old school | |
no_dups = set() | |
for n in range(1, 100): | |
for x in range(2,10): | |
if n % x == 0: | |
no_dups.add(n) | |
print(no_dups) | |
print() | |
#nested list comprehension | |
result = [number for number in range(1,100) if True in [True for x in range(2,10) if number % x == 0]] | |
print(result) |
numbers = list(range(1,1001))
divisors = list(range(2,10))
ans = [n for n in numbers if any([ n % d == 0 for d in divisors])]
print(ans)
print([*set([i for i in range(1,1000) for j in [2,3,4,5,6,7,8,9] if i % j == 0])])
result = list(set([i for i in range(1, 100) for y in range(2, 10) if i % y == 0]))
Print([i for i in range(1,1001) if True in (True for j in range(2,10) if i%j==0])
rng = range(1,1001)
twotonine = range(2,10)
divisibleby = [(num,div) for num in rng for div in twotonine if num % div == 0]
print(divisibleby)
this thread is a bit chaotic.
two remarks:
1-1000 is range(1,1001) not range(1,1000)
even the solution omits the last number by using 100 instead of 101
nested list comprehensions (to me) means one list comprehension inside of the other. None of the answers seem to have that.
Personally I got as far as
print([x for x in range(1,1001) for y in [i for i in range(2,10)] if x%y == 0])
but the list contains duplicates.
I'll study the solution some more at a later time to fully grasp what's going on.
Thanks to the people using set(), as this was new to me. It seems very useful.
Just to be a smart-arse, using the full range 2-10 is unnecessary, because if a number isn't divisible by 2, it won't be divisible by 4, 6, 8 etc.
So a simplified version of this is:
result = [n for n in range(1001) if 0 in [n % divisor for divisor in [2,3,5,7]]]
Just to be a smart-arse, using the full range 2-10 is unnecessary, because if a number isn't divisible by 2, it won't be divisible by 4, 6, 8 etc.
So a simplified version of this is:
result = [n for n in range(1001) if 0 in [n % divisor for divisor in [2,3,5,7]]]
good approach.
numbers = range(1, 1001)
div = range(2, 10)
result = [number for number in numbers if any(number % d == 0 for d in div)]
print(result)
When I read the statement without looking at the 'old school' code, I understood the objective differently. This code provides a list of lists in which each sublist contains all multiples for each of the digits 2-9. In case anyone was looking for this.
nested_list = [[number for number in range(1,1001) if number%digit == 0] for digit in range(2,10)]
print(nested_list)
"""
Use a nested list comprehension to find all of the numbers from
1-1000 that are divisible by any single digit besides 1 (2-9)
"""
divisibles = [i for i in range(1, 1001) if any([1 for j in range(2,10) if not i%j])]
Is yours wrong or is mine lol. I'm new to this. besides to me means 1-10. 1,2,3,4,5,6,7,8,9 All numbers besides these so these shouldn't be in the list right? Thanks for making this available I need the practice.
#7. Use a nested list comprehension to find all of the numbers from 1–1000 that are divisible by #any single digit besides 1 (2–9)
result = [number for number in range(1,100) if number % number == 0 if number > 9]
print(result)