Python not recognizing MAX value in a list

Click For Summary

Discussion Overview

The discussion revolves around a Python programming issue where a user is experiencing difficulty in having Python recognize the maximum value from a list of floating-point numbers. The conversation explores potential coding mistakes, platform-specific behavior, and suggestions for code optimization.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that Python does not recognize 0.6000001 as the maximum value in their list, despite it being the highest value.
  • Another participant suggests that the issue may stem from a coding mistake and provides a simple code example that successfully identifies the maximum value.
  • A participant shares their current code, which involves summing several counters and determining the majority value, while emphasizing the assumption of no duplicate values.
  • One user mentions that the same code works on a Windows platform but fails on Ubuntu, indicating potential platform-specific issues.
  • Another participant raises a concern about the condition checking for duplicates in the list, suggesting that it may lead to incorrect results if not handled properly.
  • Several participants discuss ways to optimize the lengthy if-else structure in the code, with one suggesting a method to store labels alongside their values for easier manipulation.
  • One participant expresses gratitude for the optimization suggestion, indicating it significantly reduced the amount of code needed.
  • A later reply clarifies a misunderstanding regarding the terminology used, correcting the use of "loop" to "branching or decision structure." This correction is acknowledged by the original poster.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the issue with recognizing the maximum value, as there are multiple competing views regarding potential coding mistakes and platform-specific behavior. The discussion on optimizing the code also reflects differing opinions on the best approach.

Contextual Notes

There are unresolved assumptions regarding the handling of floating-point precision and the implications of platform differences on code execution. Additionally, the discussion includes various coding practices that may not be universally applicable.

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   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
  • #10
yes, totally. adding the word loop there was misleading. thanks for pointing it out.
 
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K