Python not recognizing MAX value in a list

  • Python
  • Thread starter msn009
  • Start date
  • Tags
    python
  • #1
53
6

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
11,501
5,051
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
53
6
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
53
6
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
wle
307
136
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
53
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
wle
307
136
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:
  • #8
53
6
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
33,285
4,989
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.
 
  • #10
53
6
yes, totally. adding the word loop there was misleading. thanks for pointing it out.
 

Related Threads on Python not recognizing MAX value in a list

  • Last Post
Replies
15
Views
624
Replies
3
Views
703
Replies
5
Views
676
  • Last Post
Replies
4
Views
237
Replies
6
Views
4K
  • Last Post
Replies
10
Views
951
  • Last Post
Replies
22
Views
5K
  • Last Post
Replies
1
Views
2K
Replies
2
Views
1K
Replies
7
Views
1K
Top