0
Python min function with *args
How can i find the minimum number in a list by passing them to my own function. def myfunc(a,*b): ..........
7 odpowiedzi
+ 10
sid ,
as we don't know what your function will receive as arguments and what the function is doing exactly (? only finding the min nunbers ?), please post the complete code, and also a complete task description.
▪︎in general: for finding the min number of a list, you don't need to use *args. just use:
def myfunc(the_list)
.....
+ 7
No need to declare x in the function. Just declare *y and then use a for loop to iterate and return the minimum value. Maybe your want like this:
https://code.sololearn.com/cYzdRU49Gb0U
+ 5
Do you mean something like this?
https://code.sololearn.com/c63RYk5iMq3q
+ 5
If you want the function to return <x> when there's nothing in <y> having value less than <x>, without using built-in function
def my_min(x, *y):
for v in y:
if v < x:
x = v
return x
print(my_min(8, 13, 4, 42, 120, 7))
+ 2
Lothar
def my_min(x, *y):
if x < y:
return x
else:
return y
print(my_min(8, 13, 4, 42, 120, 7))
The given code defined a function called my_min(), which takes two arguments and returns the smaller one.
I need to improve the function, so that it can take any number of variables, so that the function call works.
+ 1
The future is now thanks to science[In a break] i have to find the minimum using my own,not with inbuilt min()
+ 1
def get_min(*args):
if all([type(i) is int for i in args]):
return min(args)
else:
return "wrong type of item in list"