Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
def divisible_by(numbers, divisor):
return [x for x in numbers if x % divisor == 0]
i know it returns the divisible numbers from the array of numbers, but can someone explain how it gets there?
I am learning python as my first language, but i have not got to arrays yet.
i am mostly confused by this part "x for x in numbers if x"
Thanks
This is what is called "list comprehension". In one line, it creates a new list of all the numbers in "numbers" that are divisible by divisor. That's what the modulo (%) is checking. It checks that the remainder of the division is equal to 0.
The list comprehension is equivalent to saying:
divisible_numbers = []
for x in numbers:
if x % divisor == 0:
divisible_numbers.append(x)
return divisible_numbers
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
n = int(input())
for x in (range(1,n))%2!=0:
if x % 3 == 0 and x % 5 == 0:
print("SoloLearn")
elif x % 3 == 0:
print("Solo")
elif x % 5 == 0:
print("Learn")
It gives this error---TypeError: unsupported operand type(s) for %: 'range' and 'int'
why can i not divide 2 by range of numbers?
you want odd numbers try this:
for x in (range(1,n,2)):
instead of:
for x in (range(1,n))%2!=0:
Python objects decide for themselves what operations mean by implementing magic methods - see Emulating Numeric Types. range(1,n) % 2 translates to calling range(1,n).__mod__. But that method isn't implemented for range objects because the authors didn't think it made sense to modulo an integer generator. Remember, this is an operation on the generator object, not the integers iterated by the generator.
If you really wanted to divide a range of numbers by 2, you could do it as the numbers are generated
>>> for x in (y % 2 for y in range(10)):
... print(x)
Are you trying to create a list of odd numbers?
YOu could try:
for x in [num for num in range(1,n) if num x%2]:
#do_stuff
note that you can lose the !=0 part of the check because non-zero integers equate to True
As noted by Sujay, there's a quicker way to do it that hides the underlying mechanism.
The example I gave you can be modified to use more complex checks.
ALSO!
Note that you don't really want to do this. You're solving fizzbuzz, you need to iterate over all numbers. Number 30 is even and you want to print 'sololearn' ('fizzbuzz') when you reach it.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
firstLst = [1,2,3,4]
secondLst = [5,6,3,2,4,1]
If i am passing the two lists above as arguements in a function. I would like to know how I can check if all elements in the firstLst are in the secondLst using recursions with Python. I can achieve this by having two extra parameters i and j which will allow me to check each element in firstLst and secondLst, but is there a way to do this by only having the two lists as parameters?
example of the function with parameters:
def firstInSecond(lst1,lst2):
How can True be returned if all elements in lst1 are in lst2 otherwise False is returned. I hope this makes sense.
This isn't exactly recursive, but simpler.
set(firstLst).issubset(set(secondLst))
For a recursive answer,
def firstInSecond(lst1,lst2):
if len(lst1) == 0:
return True
if lst1[0] in lst2:
return firstInSecond(lst1[1:], lst2)
else:
return False
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need an example of a recursive function that will append all integer elements of a number into a list using python and doing this with the most basic algorithm.
For example if n = 1234, list will be [1,2,3,4].
Try this?
def numberToList(number):
# base case
if number == 0:
return []
# recurse
return numberToList(number / 10) + [ number % 10 ]
Run it like this:
>>> numberToList(1234)
[4, 3, 2, 1]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I find the smallest odd number in a list of integers, if the problem statement says that I can't use for and if?
You can use filter and min:
s = [4, 2, 3, 4, 7, 1]
smallest_odd = min(filter(lambda x:x%2 != 0, s))
Output:
1
Why should anyone consider using for or if?
min(numbers, key=lambda n: (n&1==0, n))
you can use
ints = [2,6,8,4,6 ]
srt = sorted(ints)
while len(srt) > 0 and not srt[0] % 2:
srt = srt[1:]
s = None
while len(srt):
s = srt[0]
srt=[]
print(s)
to circumvent the rules. You sort the list, discard any even values from front.
The resulting list is either empty or has the value at front.
The second while is only entered if the list contains elements, and the list is reset to empty (which is false and will not renter the while).
It will print "None" or the lowest not even number.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write a function called sumDivs that accepts a single, positive integer and calculates the
sum of all of its proper divisors. Use a for loop to find and sum the proper divisors of the
given integer.
For example,
>>> sumDivs(8)
7
this is what i have:
def sums(n):
i=0
for i in range (1, n-1):
if n % i == 0:
return n-1
i+=1
You shouldn't return the first divisor you find, you should be adding them as you go; i takes on the values from 1 to n-2 in your loop change to for i in range(1,n); and there is no need to define an initial value for i. Try this instead:
def sums(n):
sum=0
for i in range (1, n):
if n % i == 0:
sum+=i
return sum