# How to check that none of the values are equal?

• Python

## Main Question or Discussion Point

the code below intends to take the maximum value that is calculated for each of the variable but after the max value is obtained I am not sure how to ensure there are no 2 or more values are the same in the if else loop as if 'a' has the max value of 3 and 'b' also has the max value of 3 then the if loop spits out 'a' but i want it to go to the else loop if there are equal values.

Code:
majority = max( a, b, c, d)

if majority != 0:
if a == majority:
df['label'] = 'aaaaaa'
elif b== majority:
df['label'] = 'bbbbbb'
elif c == majority:
df['label'] = 'cccccc'
elif  d== majority:
df['label'] = 'ddddd'

else:
row['label'] = 'no_label'

Related Programming and Computer Science News on Phys.org
DrClaude
Mentor
What you can do is to replace the "else if"'s by simple if's, and add a counter that you set to 0 before the "if a ==", and increment each time one of the conditions is satisfied. That way, after the series of if's, you'll know how many variables were equal to the max.

thanks. sorry the last part should be df['label'] = 'no_label' and i just found a solution that could work as below:

Code:
majority = max( a, b, c, d)

if majority != 0 and (a != b != c != d):
if a == majority:
df['label'] = 'aaaaaa'
elif b== majority:
df['label'] = 'bbbbbb'
elif c == majority:
df['label'] = 'cccccc'
elif  d== majority:
df['label'] = 'ddddd'

else:
df['label'] = 'no_label'

DrClaude
Mentor
I don't know python, but I would be very surprised that
Python:
(a != b != c != d)
would return what you want.

i just checked again and it will not work for say if a = 4 and c = 4. i am trying to avoid using too many if else condition so the idea is to just be able to check that none of the a,b,c,d values are the same so that it only works when there is a majority value

i tried this below and it seems to do the job of checking for duplicates.
Code:
majority = max( a, b, c, d)
listmaj = (a,b,c,d)

if majority != 0 and len(set(listmaj) == len(listmaj):
if a == majority:
df['label'] = 'aaaaaa'
elif b== majority:
df['label'] = 'bbbbbb'
elif c == majority:
df['label'] = 'cccccc'
elif  d== majority:
df['label'] = 'ddddd'

else:
df['label'] = 'no_label'

pbuk
Gold Member
I don't know python, but I would be very surprised that
Python:
(a != b != c != d)
would return what you want.
You clearly don't know Python - this is indeed an unusual feature of the language.
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
However I avoid using this for two reasons:
• It is confusing to anyone who does not work exclusively in Python.
• Python allows operator overloading so I'm not sure that it is guaranteed to work if the operators are overloaded: it should do, because it is implemented at the parser level, parsing internally to ((a != b) and (b != c) and (c != d)) before compiling, but why rely on that when it is as easy to do the parsing yourself?

PeroK
Homework Helper
Gold Member
the code below intends to take the maximum value that is calculated for each of the variable but after the max value is obtained I am not sure how to ensure there are no 2 or more values are the same in the if else loop as if 'a' has the max value of 3 and 'b' also has the max value of 3 then the if loop spits out 'a' but i want it to go to the else loop if there are equal values.
Note that, in general, a programme cannot be logically simpler than the process you are coding. In this case, I assume that you want to print out all the maximum values. This, logically, has three steps:

Find the maximum value
Identify all the variables that have that maximum value
Print a line listing all the variables that have the maximum value

Your issue is partly trying to squeeze a three-step process into two steps. This is often where complexity and bugs arise in coding.

In this case, therefore, I would look at having more lines of code and a three-step process to model more accurately the logical process you are trying to code.

the idea is to check if any of the value in the list has duplicates in it then to skip to the else loop if a majority value cannot be found. so i have already achieved this need but I agree with you that the better way would have been to create a stand alone definion to check for duplicate values instead of doing it all at once in one line of code but as my priority now is for implementation i am just using what works,

DrClaude
Mentor
You clearly don't know Python - this is indeed an unusual feature of the language.

However I avoid using this for two reasons:
• It is confusing to anyone who does not work exclusively in Python.
• Python allows operator overloading so I'm not sure that it is guaranteed to work if the operators are overloaded: it should do, because it is implemented at the parser level, parsing internally to ((a != b) and (b != c) and (c != d)) before compiling, but why rely on that when it is as easy to do the parsing yourself?
But the way it is written, wouldn't return true if $a \neq b$ but $a = c$ or $a = d$, for instance?

Ibix
Does this not work?
Python:
value_list=(a,b,c,d)
maxval=max(value_list)
multiple_max=(len(v for v in value_list if v==maxval)>1)

Svein
I would
1. Sort the values in descending order (in an array A[0..n])
2. Then A[0] is the maximum
3. If A[1]=A[0], there are two maximum values
4. If A[2]=A[0], there are three maximum values
5. etc.

jbriggs444
Homework Helper
2019 Award
Does this not work?
Python:
value_list=(a,b,c,d)
maxval=max(value_list)
multiple_max=(len(v for v in value_list if v==maxval)>1)
I am not a python programmer, but you seem to have used the wrong sort of braces there.

>>> print value_list
(1, 2, 3, 4)
>>> print maxval
4
>>> test = (v for v in value_list if v==maxval)
>>> print test
<generator object <genexpr> at 0x7f853d90b960>
>>> print len(test)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'generator' has no len()

>>> test = [v for v in value_list if v==maxval]
>>> print test
[4]
>>> print len(test)
1

Ibix
I am not a python programmer, but you seem to have used the wrong sort of braces there.
Thanks. I thought it would work without the conversion to list, but I didn't have python to hand to check.

sum(map(lambda v: v==maxval,value_list))>1 ought to work too.

jbriggs444
Homework Helper
2019 Award
Thanks. I thought it would work without the conversion to list, but I didn't have python to hand to check.

sum(map(lambda v: v==maxval,value_list))>1 ought to work too.
Confirmed.
>>> print sum(map(lambda v: v==maxval,value_list))
1

Ibix
Confirmed.
>>> print sum(map(lambda v: v==maxval,value_list))
1
Probably worth checking it in a case where value_list does have a repeated maximum value before saying it's confirmed! I think python coerces booleans to ints if you sum them, but again I don't have python to hand to check. I must see if there's a version for Android.

jbriggs444
Homework Helper
2019 Award
Probably worth checking it in a case where value_list does have a repeated maximum value before saying it's confirmed! I think python coerces booleans to ints if you sum them, but again I don't have python to hand to check. I must see if there's a version for Android.
>>> print value_list
(1, 2, 3, 3, 2, 1)
>>> print maxval
3
>>> print sum(map(lambda v: v==maxval,value_list))
2
>>> print sum(map(lambda v: v==maxval,value_list))>1
True

Edit: A momentary confusion while testing an empty value_list led to an incorrect test result now editted away.

Last edited:
wle
You can find the maximum element and the number of times it appears in a list using functions and methods that are already built-in in Python:
Python:
numbers = [1, 3, 2, 8, 3, 8, 7, 4]
x = max(numbers)       # x = 8
n = numbers.count(x)   # n = 2
It's also easy to write a function that finds both in one search:
Python:
def max_and_count(numbers):
maxn = numbers[0]
count = 1

for n in numbers[1:]:
if n > maxn:
maxn = n
count = 1
elif n == maxn:
count += 1

return maxn, count

# Use:
numbers = [1, 3, 2, 8, 3, 8, 7, 4]
x, n = max_and_count(numbers)   # x = 8, n = 2