简体   繁体   中英

How do i add the numbers of multiples in a list?

Im trying to find the number of multiples in a list while divided by a number provided from the user. For example the list [12, 5] and the user input is 3 so the output would be 5, because int(12/3) + int(5/3) = 5.

def multiple(first_list, dividing_num):
     quotient = []
     for i in list(first_list):
      quotient = i // dividing_num
      i += quotient
     return i
print(multiple([9, 18, 2, 20, 21], 4))
#the output should be 16

few changes in your for loop

summ = 0
for i in first_list:
    quotient = i // dividing_num
    summ += quotient
return summ

quotient = [] can be removed

You can actually use a very short comprehension here:

l = [12, 5]
n = 3
sum(i//n for i in l)

output: 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM