Reputation: 27
I need to find the max value of a list but I can't use the max() method. How would I go about doing this? I'm just a beginner so I'm sure this is pretty easy to find a workaround, however I'm stumped.
Edit: Forgot to mention that it's python I'm working in.
Upvotes: 0
Views: 67494
Reputation: 3899
This is a code implementation of the algorithm described in @OlympusMonds answer. This is useful if you want to find the max value in a list without using the max() function or any other builtin:
num_list = [3, 1, 8, 0, 5, 9, 2, 4, 6, 7]
max_val = num_list[0] # Initialise max_val and set it to the first item in the list
for num in num_list:
if num > max_val:
max_val = num
You can print(max_val)
to see that it returns the maximum value in the list. In this case, the output is 9
.
Upvotes: 0
Reputation: 26
Here, we use the reduce function that is available in the built-in module called "functools", essentially "reduce" returns only one value, which in this case, is the maximum of the numbers. So, the main logic here is we return the current number if it's larger than the next number in the list, else we just return the next number if it doesn't satisfy the condition.
from functools import reduce
inp = input("Values separated by spaces: ")
arr = map(int, inp.split())
ans = reduce(lambda x, y: x if x > y else y, arr)
print(ans)
Upvotes: 1
Reputation: 1
s=[10,11,12,9,10,11]
length = len(s)-1
for i in range(length):
if s[i] > s[i + 1]:
s[i], s[i + 1] = s[i + 1], s[i]
print(s[-1]) #12
Upvotes: -1
Reputation: 1
input_string =input("Enter a list numbers or elements separated by space: ")
userList = input_string.split()
l=list((map(int, userList)))
print(l)
a = (len(l))
temp=0
for i in range(0,a-1):
if l[i]>l[i+1]:
c=l[i]
if c>temp:
temp=c
else:
c= l[i+1]
if c>temp:
temp=c
print(temp)
Upvotes: -2
Reputation: 1
You could try this.....
my_list = [0, 2, 5, 6, 5, 8, 5, 8, 8]
my_max = [ ]
for list in my_list:
if list > 7:
my_max = my_max + [list]
Print(my_max)
This should output [8,8,8]
Upvotes: -2
Reputation: 95
def max(a):
res = a[0]
for i in a:
if res < i:
res = i
return res
array = (1, 2, 3, 4, 5, 6)
print(max(array))
Upvotes: -2
Reputation: 13529
Simple one-liner in python:
max = sorted(my_list)[-1]
This puts the list in order from smallest to largest, then takes the final value which is the largest.
Upvotes: 1
Reputation: 3674
max = list[0]
for x in list:
if x > max:
max = x
print max
In this example, we initialize the max value to the first element. Then we iterate through the list, and if we find a larger value than the current max, we assign that value to max. At the end, we should have the largest value, which we print.
Upvotes: 10
Reputation: 158
Think about how you would do this manually.
If you had a list like this:
[10, 6, 8, 2, 4, 12, 3]
To find the max manually, you would start with the first number, 10, and that would be the max. Then you move to the next number, 6:
M
[10, 6, 8, 2, 4, 12, 3]
^
Is it bigger than 10? No, then move to the next number:
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
M
[10, 6, 8, 2, 4, 12, 3]
^
Now we come to a number that IS bigger than our max of 10. So then we move the max point:
M
[10, 6, 8, 2, 4, 12, 3]
^
Then continue:
M
[10, 6, 8, 2, 4, 12, 3]
^
The list is finished now, and M points at 12.
Now you just need to code it up!
Upvotes: 7
Reputation: 104
Sounds like homework to me, but perhaps some logics can help you out:
when you iterate the list, the point is that you want to know whether the current element is the MAX of all others. This can be solved by keeping the MAX up to the current element (say M*) and do a simple comparison between M* and your current element. Then update M* according to the result of that comparison. At the end of the iteration I think you can figure out where your MAX is.
Upvotes: 1