Python not recognizing MAX value in a list

In summary: if counter_e == majority: row['new_label'] = 'e'max value = 0
  • #1
53
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
  • #2
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.
 
  • #3
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'
 
  • #4
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
 
  • #5
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.
 
  • #6
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'
 
  • #7
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
  • #8
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!
 
  • #9
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

1. Why is Python not recognizing the MAX value in my list?

There could be several reasons for this issue. One possibility is that the list contains non-numeric values, which cannot be compared to determine the maximum value. Another possibility is that the list is empty, in which case there is no maximum value to be found.

2. How can I fix Python not recognizing the MAX value in my list?

If the issue is due to non-numeric values in the list, you can filter them out using the filter() function or by creating a new list with only the numeric values. If the issue is due to an empty list, you can add values to the list before trying to find the maximum.

3. Can I use a custom function to find the maximum value in a list?

Yes, you can define a custom function and use it to find the maximum value in a list. However, make sure that the function can handle non-numeric values and empty lists to avoid any errors.

4. Is there a built-in function in Python to find the maximum value in a list?

Yes, the max() function can be used to find the maximum value in a list. However, as mentioned before, it may not work if the list contains non-numeric values or is empty.

5. How can I avoid future issues with Python not recognizing the MAX value in a list?

To avoid any issues, make sure to thoroughly check your list for non-numeric values and empty lists before using the max() function. You can also consider using a try-except block to handle any potential errors that may arise.

Suggested for: Python not recognizing MAX value in a list

Replies
4
Views
1K
Replies
2
Views
613
Replies
5
Views
1K
Replies
43
Views
3K
Replies
2
Views
913
Replies
23
Views
1K
Replies
1
Views
155
Replies
3
Views
1K
Back
Top