How to check that none of the values are equal?

  • Context: Python 
  • Thread starter Thread starter msn009
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around a coding problem in Python related to determining the maximum value among a set of variables and ensuring that no two or more values are equal. Participants explore various methods to implement this logic within an if-else structure, focusing on how to handle cases where multiple variables may share the maximum value.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses uncertainty about how to ensure that the if-else loop does not select a variable if there are equal maximum values.
  • Another suggests using a counter to track how many variables are equal to the maximum value, allowing for a check after the conditions.
  • A different approach is proposed, using a set to check for duplicates among the variables before proceeding with the if-else structure.
  • Some participants question the validity of using chained comparisons in Python, suggesting it may not behave as expected in all cases.
  • One participant mentions that sorting the values could help identify duplicates and the maximum value more clearly.
  • Another participant points out a potential issue with using a generator expression for counting occurrences of the maximum value, suggesting that converting it to a list may be necessary.
  • Several participants discuss the use of built-in functions to find the maximum value and count its occurrences, indicating a preference for more efficient solutions.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to solve the problem. Multiple competing views and methods are presented, with some participants expressing skepticism about certain proposed solutions.

Contextual Notes

Some participants note that the complexity of the problem may stem from trying to condense a multi-step logical process into a simpler code structure, which could lead to bugs and confusion.

msn009
Messages
53
Reaction score
6
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'
 
Technology news on Phys.org
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'
 
I don't know python, but I would be very surprised that
Python:
(a != b != c != d)
would return what you want.
 
  • Like
Likes   Reactions: PeroK
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'
 
DrClaude said:
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?
 
msn009 said:
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.
 
  • Like
Likes   Reactions: QuantumQuest
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,
 
  • #10
MrAnchovy said:
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?
 
  • #11
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)
 
  • #12
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.
 
  • #13
Ibix said:
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
 
  • Like
Likes   Reactions: Ibix
  • #14
jbriggs444 said:
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.
 
  • Like
Likes   Reactions: jbriggs444
  • #15
Ibix said:
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
 
  • Like
Likes   Reactions: Ibix
  • #16
jbriggs444 said:
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.
 
  • Like
Likes   Reactions: PeroK
  • #17
Ibix said:
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:
  • Like
Likes   Reactions: Ibix
  • #18
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
 
  • Like
Likes   Reactions: Ibix

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K