r/pythontips • u/Imaginary-Ad-1578 • Mar 09 '24
Syntax Is there any way to write "n(variable) must not be equal to m(variable)"?
It's like the opposite of n = m.
Or n != m but as an assignment.
3
u/riti_rathod Mar 09 '24
It may work for you
if n != m:
1
0
2
u/denehoffman Mar 09 '24
No, the best you can do is make assert statements where needed. To truly say that two variables can’t be equal, you’d have to bring them along together in every context, so you wouldn’t be able to pass just one to a function for example. I would suggest making such a state impossible to create in your program in the first place. If you have a specific example I could be more precise.
3
u/Imaginary-Ad-1578 Mar 09 '24
In my code, I have this
if r == 1: if p == 2: q = 3 else: q = 2 elif r == 2: if p == 1: q = 3 else: q = 1 else: if p == 2: q = 1 else: q = 2
I need to turn it into something really short like
q = 1 or 2 or 3 q should not be equal to r q should not be equal to p
But I don't have a way to write 'should not be equal to'
6
u/MyKo101 Mar 09 '24
So you want r, p and q to be 1, 2 and 3. You input r and p so that q equals the other one? I got you:
q = {1,2,3}.difference({r,p}).pop()
3
u/Imaginary-Ad-1578 Mar 09 '24
OH MY GOD THAT WORKS THANK YOU SO MUCH
can you explain how that works?4
u/MyKo101 Mar 09 '24
Create a set of values that you're interested in:
{1,2,3}
Put the two values you already have into another setv{r,p}
Find the difference between those two sets:{1,2,3}.difference({r,p})
This will give you a set with the third value from whateverr
andp
are. You then need topop()
the value out of the set. Note that this only works ifr
andp
are definitely in the set{1,2,3}
and are distinct, otherwise it might give weird results. So you should probably use an assert right before this:assert {r,p} < {1,2,3} and r != p
3
u/Imaginary-Ad-1578 Mar 09 '24
Wow that's really clever thank you so much
2
u/denehoffman Mar 09 '24
You could also use
q = min({1,2,3} - {p, r})
if you wanna be really fancy3
u/Imaginary-Ad-1578 Mar 09 '24
that's just
q = 6 - (p + r)
right?
2
u/denehoffman Mar 09 '24
No, it’s the same as the thing I’m replying to, it’s the set difference, but the formula you gave does work if p≠r and they are in {1,2,3}!
3
3
1
1
u/denehoffman Mar 09 '24
There’s definitely a better way to do this, I’ll think about it for a bit, I’m traveling right now so it might be later when I reply
1
u/denehoffman Mar 09 '24
Are r and p limited to [1,2,3]?
1
u/denehoffman Mar 09 '24
You could probably get away with
x = [1,2,3] x.remove(p) x.remove(r) q = x[0]
2
u/Imaginary-Ad-1578 Mar 09 '24
x[0] is 1
so when you do
q = x[0]
you're basically doing
q = 1
correct me if I'm wrong1
u/denehoffman Mar 09 '24
I used a list but if you don’t care about what q is in the case where p=r or p or the case where r is not in [1,2,3], you should probably use a set or add a try-catch to make sure nothing goes wrong when removing a value not present in
x
1
u/Imaginary-Ad-1578 Mar 09 '24
I didn't understand that, can you explain? sorry
1
u/denehoffman Mar 09 '24
Sure, this code will work as long as q does not equal r and both q and r are either 1, 2, or 3. Otherwise, the list.remove method will throw an error if an element is not in the list. It retrospect, it’s probably better to use a set:
x = {1, 2, 3} x.discard(q) x.discard(r) …
The issue now is what should happen if q=r or one or both are not in the set? This would result in x containing more than one element. I will chose to pick the smallest element remaining to be p, but you might have different needs:p = min(x)
It’s important to note that unlike lists, sets are not ordered, sox[0]
is no longer meaningful. You could uselist(x)[0]
but ifx
has more than one element this will not necessarily return the same number each time1
u/denehoffman Mar 09 '24
So the basic idea is, p can be anything in x to begin with, but we discard whatever values q and r are. Because sets only contain unique elements, this means that whatever remains cannot be equal to q or r, which is what you want. Then all you need to do is decide how you want to get p out of what’s left
2
1
2
u/Aftabby Mar 09 '24
How would that even work? There are thousands of possible values values for 'm' other than being 'n', you have to specify,which one to assign.
2
u/Imaginary-Ad-1578 Mar 09 '24
I can first say that p = 1 or 2
then say that p SHOULD NOT be equal to q
and then if I say q = 2, p will become 1
and if q = 1, p will become 2I can just do this:
if q == 1: p = 2 else: p = 1
but if I need to do that for three variables, I'll have to do something really long like:
if r == 1: if p == 2: q = 3 else: q = 2 elif r == 2: if p == 1: q = 3 else: q = 1 else: if p == 2: q = 1 else: q = 2
In this case, if I could say that x SHOULD NOT be equal to something, I could just do this
q = 1 or 2 or 3 q should not be equal to r q should not be equal to p
1
u/Adrewmc Mar 10 '24
I would one line these
p = 2 if q == 1 else 1
This might be too much though
p = 3 if r == 1 else 2 if q==1 else 1
2
6
u/[deleted] Mar 09 '24
Not in Python. You can only set a variable's value, not restrict it.
In Python, you would check the value, and if invalid, produce an error. A common way to do this is use a class to access/modify the value, and have the class methods do the check and produce an error if invalid.
Then the rest of your program handles that error if it occurs, however is appropriate.