简体   繁体   中英

Numpy calculating the divisible whole numbers and remainder Python

How can I write a numpy function that lists the number of times a number is a factor of another number with the remainder if there are any. So number 1 and 2 are being divided by 10 and the expected result . So when number2 is divided by the divisor there is a whole number of 7 divisor values and a remainder of 2 which is seen in the expected results . I do not know how I can go about doing this function?

def calculating(number, divisor):
   

number = 70
number2 = 72
number3 = 7
divisor = 10

calculating(number2, divisor)
calculating(number2, divisor)

Expected Result:

number1= [10 10 10 10 10 10 10]
number2= [10 10 10 10 10 10 10 2]
number3= [7]
def calculating(number, divisor):
    list1=[]
    while number - divisor > 0:
        list1.append(divisor)
        number-=divisor
    list1.append(number)
    return list1
print(calculating(70,10))
#[10, 10, 10, 10, 10, 10, 10]
print(calculating(72,10))
#[10, 10, 10, 10, 10, 10, 10, 2]
print(calculating(7,10))
#[7]

recursion

def calculating(number, divisor,list1=[]):
    if number - divisor > 0:
        list1.append(divisor)
        number-=divisor
        calculating(number, divisor,list1)
    else:
        list1.append(number)
    return list1

print(calculating(72,10))
#[10, 10, 10, 10, 10, 10, 10, 2]

If you do not need the fastest possible implementation the following is short and easy to understand:

def calculating(number, divisor):
    return np.diff(np.r_[:number:divisor, number])

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