Python Python not recognizing MAX value in a list

AI Thread Summary
The discussion revolves around issues with Python not recognizing the maximum value from a list of floating-point numbers, specifically the value 0.6000001. A user shared their code and expressed frustration that it worked on Windows but not on Ubuntu. Other participants suggested that the problem might stem from coding mistakes rather than the data itself, emphasizing the importance of reviewing the code structure. A sample code snippet was provided to illustrate how to find the maximum value correctly. Additionally, suggestions were made to simplify the code that assigns labels based on maximum values, highlighting the efficiency of using dictionaries to store data alongside their labels. This approach significantly reduced the complexity of the original if-else statements, demonstrating a more efficient way to handle the logic. The conversation also clarified the difference between loops and branching structures in programming.
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.
 
Technology 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 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 jedishrfu
  • #10
yes, totally. adding the word loop there was misleading. thanks for pointing it out.
 
  • Like
Likes jedishrfu

Similar threads

Replies
2
Views
2K
Replies
15
Views
2K
Replies
29
Views
3K
Replies
23
Views
2K
Replies
6
Views
3K
Replies
4
Views
5K
Back
Top