Python not recognizing MAX value in a list

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 2K views
msn009
Messages
53
Reaction score
6
i have a value that is generated as the max value for example from this list [0.6000001, 0.3, 0.3, 0.4]

clearly the maximum value is 0.6000001 but python does not seem to be able to recognize this value as the maximum. i have tried so many ways to solve it but to no avail. even when changing 0.2+0.2+0.2 to 2+2+2 it is still the same problem.
 
Physics news on Phys.org
You need to show us your code. You most likely have a coding mistake and changing the data doesn't cover it.

You could play with this example:

Python:
max=0
for n in [0.6000001, 0.3, 0.3, 0.4]:
  if(n>max):
    max=n

print("max value = "+str(max))

It works in my example.
 
This is how the code looks like now. just assume there are no duplicate values as i just want to solve finding the maximum value. thanks

Python:
                                counter_a = np.sum(counter_a)                                 
                                counter_b = np.sum(counter_b)
                                counter_c = np.sum(counter_c)
                                counter_d = np.sum(counter_d)
                                counter_e = np.sum(counter_e)

                                majority = max(counter_a, counter_b, counter_c, counter_d,counter_e)
                                list_maj = [counter_a, counter_b, counter_c, counter_d,counter_e]

                                #if list does not have duplicate values
                                if majority != 0 and (len(list_maj) == len(set(list_maj))):
                                    if counter_a == majority:
                                        row['new_label'] = 'a'
                                    if counter_b == majority:
                                        row['new_label'] = 'b'
                                    if counter_c == majority:
                                        row['new_label'] = 'c'
                                    if counter_d == majority:
                                        row['new_label'] = 'd'
                                    if counter_e== majority:
                                        row['new_label'] = 'e'
 
it is funny that when i tried this very same code on a windows platform it did not give me any problem but when i ran it on a ubuntu platform, it will just not read that 0.600000001 as the maximum value
 
Did you run your code where some values other than the maximum are duplicated? E.g. if counter_a = 0.6000001, counter_b = 0.3, and counter_c = 0.3 then the test
len(list_maj) == len(set(list_maj))

will return False.
 
below is the complete code considering when there are no duplicates and with duplicates. i would welcome suggestions on how to shorten this whole if else loop :)

Python:
# if list does not have duplicate values
if majority != 0 and (len(list_maj) == len(set(list_maj))):
    if counter_a == majority:
        row['new_label'] = 'a'
    if counter_b == majority:
        row['new_label'] = 'b'
    if counter_c == majority:
        row['new_label'] = 'c'
    if counter_d == majority:
        row['new_label'] = 'd'
    if counter_e == majority:
        row['new_label'] = 'e'

# if list has duplicate values. If there are 2 elements with same maximum value, take both
if majority != 0 and (len(list_maj) != len(set(list_maj))):
    if counter_a == majority and list_maj.count(majority) == 1:
        row['new_label'] = 'a'
    elif counter_a == majority and counter_a == counter_b:
        row['new_label'] = 'a, b'
    elif counter_a == majority and counter_a == counter_c:
        row['new_label'] = 'a, c'
    elif counter_a == majority and counter_a == counter_d:
        row['new_label'] = 'a,d'
    elif counter_a == majority and counter_a == counter_e:
        row['new_label'] = ‘a,e’

    elif counter_b == majority and list_maj.count(majority) == 1:
        row['new_label'] = 'b'
    elif counter_b == majority and counter_b == counter_a:
        row['new_label'] = 'a,b'
    elif counter_b == majority and counter_b == counter_c:
        row['new_label'] = 'b,c'
    elif counter_b == majority and counter_b == counter_d:
        row['new_label'] = 'b,d'
    elif counter_b == majority and counter_b== counter_e:
        row['new_label'] = 'b,e'

    elif counter_c == majority and list_maj.count(majority) == 1:
        row['new_label'] = 'c’
    elif counter_c == majority and counter_c == counter_a:
        row['new_label'] = 'a, c'
    elif counter_c == majority and counter_c == counter_b:
        row['new_label'] = 'b, c'
    elif counter_c == majority and counter_c == counter_d:
        row['new_label'] = 'd, c'
    elif counter_c == majority and counter_c == counter_e:
        row['new_label'] = 'e, c'

    elif counter_d == majority and list_maj.count(majority) == 1:
        row['new_label'] = 'd'
    elif counter_d == majority and counter_d == counter_a:
        row['new_label'] = 'd, a'
    elif counter_d == majority and counter_d == counter_b:
        row['new_label'] = 'd, b'
    elif counter_d == majority and counter_d == counter_c:
        row['new_label'] = 'd, c'
    elif counter_d == majority and counter_d == counter_e:
        row['new_label'] = 'd, e'    elif counter_e == majority and list_maj.count(majority) == 1:
        row['new_label'] = 'e'
    elif counter_e == majority and counter_e == counter_a:
        row['new_label'] = 'e, a'
    elif counter_e == majority and counter_e == counter_b:
        row['new_label'] = 'e, b'
    elif counter_e == majority and counter_e == counter_c:
        row['new_label'] = 'e, c'
    elif counter_e == majority and counter_e == counter_d:
        row['new_label'] = 'e, d'
    else:
        row['new_label'] = 'no_label'
else:
    row['new_label'] = 'no_label'
 
If labels like 'a', 'b', etc. are significant then you should store them as data alongside their values as soon as possible so that you can manipulate them as data. E.g.:
Python:
data = {'a': 5, 'b': 3, 'c': 5, 'd': 4, 'e': 1}
max_val = max(data.values())                                    # max_val = 5
label = ', '.join(k for k, v in data.items() if v == max_val)   # label = 'a, c'
 
Last edited:
  • Like
Likes   Reactions: msn009
wle said:
If labels like 'a', 'b', etc. are significant then you should store them as data alongside their values as soon as possible so that you can manipulate them as data. E.g.:
Python:
data = {'a': 5, 'b': 3, 'c': 5, 'd': 4, 'e': 1}
max_val = max(data.values())                                    # max_val = 5
label = ', '.join(k for k, v in data.items() if v == max_val)   # label = 'a, c'

you are awesome! thank you so much as just these few lines reduced all those lines significantly. I owe you one!
 
msn009 said:
i would welcome suggestions on how to shorten this whole if else loop :)
Just for the record, if ... elif is not a loop. A loop (such as while loop or for loop) repeats a block of code. A branching or decision structure merely chooses one option among two or more options.
 
  • Like
Likes   Reactions: jedishrfu
yes, totally. adding the word loop there was misleading. thanks for pointing it out.
 
  • Like
Likes   Reactions: jedishrfu