[Tutor] How to verify all things are equal to one another
jfouhy@paradise.net.nz
jfouhy at paradise.net.nz
Sun May 15 12:19:36 CEST 2005
Quoting Terry Carroll <carroll at tjc.com>:
> Suppose I have several variables, e.g.: a, b, c, d, e, f, g.
>
> I would like to be able to see if they're all the same, I don't care
> what the value is, as long as they're equal. If they're all equal to 0, or
> to "spam", or to ["cleese", "idle", "gilliam"], as long as they're the
> same.
Two suggestions ---
First, you can actually do multiple equality testing the way your mathematics
teacher would:
if a == b == c == d == e == f == g:
# do stuff
(this grates against my instincts in some ways, since it breaks associativity
(because it means 'a == b == c' is different from '(a == b) == c'), but it's in
the language, so I guess it's the Right Way).
You could also do this:
# python2.3: use Set module
if set([a,b,c,d,e,f,g]) == set([a]):
# do stuff
HTH.
--
John.
More information about the Tutor
mailing list