Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104883 > unrolled thread
| Started by | jj0gen0info@gmail.com |
|---|---|
| First post | 2016-03-14 17:26 -0700 |
| Last post | 2016-03-15 19:54 -0700 |
| Articles | 12 — 7 participants |
Back to article view | Back to comp.lang.python
Use of Lists, Tupples, or Sets in IF statement. jj0gen0info@gmail.com - 2016-03-14 17:26 -0700
Re: Use of Lists, Tupples, or Sets in IF statement. Joel Goldstick <joel.goldstick@gmail.com> - 2016-03-14 20:31 -0400
Re: Use of Lists, Tupples, or Sets in IF statement. Chris Angelico <rosuav@gmail.com> - 2016-03-15 11:33 +1100
Re: Use of Lists, Tupples, or Sets in IF statement. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 00:38 +0000
Re: Use of Lists, Tupples, or Sets in IF statement. jj0gen0info@gmail.com - 2016-03-14 18:32 -0700
Re: Use of Lists, Tupples, or Sets in IF statement. jj0gen0info@gmail.com - 2016-03-14 18:24 -0700
Re: Use of Lists, Tupples, or Sets in IF statement. Steven D'Aprano <steve@pearwood.info> - 2016-03-15 13:10 +1100
Re: Use of Lists, Tupples, or Sets in IF statement. Rustom Mody <rustompmody@gmail.com> - 2016-03-14 23:30 -0700
Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Peter Otten <__peter__@web.de> - 2016-03-15 09:29 +0100
Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Rustom Mody <rustompmody@gmail.com> - 2016-03-15 04:08 -0700
Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Peter Otten <__peter__@web.de> - 2016-03-15 18:34 +0100
Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. Rustom Mody <rustompmody@gmail.com> - 2016-03-15 19:54 -0700
| From | jj0gen0info@gmail.com |
|---|---|
| Date | 2016-03-14 17:26 -0700 |
| Subject | Use of Lists, Tupples, or Sets in IF statement. |
| Message-ID | <fcd34352-44d2-4912-b76c-7b3860c4fe1e@googlegroups.com> |
In Python is it possible to comparison-equate a variable to a List, Tupple, or Set and have it return True if the contents of the variable matches an element in the List, Tupple, or Set.
E.g.
x = "apple"
x-list = ["apple", "banana", "peach"]
If x == x-list:
print('Comparison is True')
else:
print('Comparison is False')
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2016-03-14 20:31 -0400 |
| Message-ID | <mailman.141.1458001918.12893.python-list@python.org> |
| In reply to | #104883 |
On Mon, Mar 14, 2016 at 8:26 PM, <jj0gen0info@gmail.com> wrote:
> In Python is it possible to comparison-equate a variable to a List,
> Tupple, or Set and have it return True if the contents of the variable
> matches an element in the List, Tupple, or Set.
>
> E.g.
>
> x = "apple"
>
> x-list = ["apple", "banana", "peach"]
>
> If x == x-list:
> print('Comparison is True')
> else:
> print('Comparison is False')
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
if x in x-list
--
Joel Goldstick
http://joelgoldstick.com/ <http://joelgoldstick.com/stats/birthdays>
http://cc-baseballstats.info/
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-03-15 11:33 +1100 |
| Message-ID | <mailman.142.1458001996.12893.python-list@python.org> |
| In reply to | #104883 |
On Tue, Mar 15, 2016 at 11:26 AM, <jj0gen0info@gmail.com> wrote:
> In Python is it possible to comparison-equate a variable to a List, Tupple, or Set and have it return True if the contents of the variable matches an element in the List, Tupple, or Set.
>
> E.g.
>
> x = "apple"
>
> x-list = ["apple", "banana", "peach"]
>
> If x == x-list:
> print('Comparison is True')
> else:
> print('Comparison is False')
Yep! What you're looking for is the "membership" operator. It's spelled "in":
>>> x = "apple"
>>> x_list = ["apple", "banana", "peach"]
>>>
>>> if x in x_list:
... print("That is a fruit.")
... else:
... print("That is not a fruit.")
...
That is a fruit.
>>> x = "jump"
>>> if x in x_list:
... print("That is a fruit.")
... else:
... print("You must be Chell.")
...
You must be Chell.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-03-15 00:38 +0000 |
| Message-ID | <mailman.143.1458002340.12893.python-list@python.org> |
| In reply to | #104883 |
On 15/03/2016 00:26, jj0gen0info@gmail.com wrote:
> In Python is it possible to comparison-equate a variable to a List, Tupple, or Set and have it return True if the contents of the variable matches an element in the List, Tupple, or Set.
>
It's actually "tuple", but what the heck :)
> E.g.
>
> x = "apple"
>
> x-list = ["apple", "banana", "peach"]
>
> If x == x-list:
> print('Comparison is True')
> else:
> print('Comparison is False')
>
if x in x-list:
...
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | jj0gen0info@gmail.com |
|---|---|
| Date | 2016-03-14 18:32 -0700 |
| Message-ID | <e4fe275e-1fbf-4b3b-aaf2-13abb6ad4e46@googlegroups.com> |
| In reply to | #104887 |
Thought "Tupple" looked wrong, too lazy to look in the book - lol.
[toc] | [prev] | [next] | [standalone]
| From | jj0gen0info@gmail.com |
|---|---|
| Date | 2016-03-14 18:24 -0700 |
| Message-ID | <a1b1ee1d-c0bf-4d43-acfc-e62a082cf883@googlegroups.com> |
| In reply to | #104883 |
Thanks to all for the responses. Very new to Python, and thought there should be a way to do it. JJ
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-03-15 13:10 +1100 |
| Message-ID | <56e76efc$0$1606$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #104883 |
On Tue, 15 Mar 2016 11:26 am, jj0gen0info@gmail.com wrote:
> In Python is it possible to comparison-equate a variable to a List,
> Tupple, or Set and have it return True if the contents of the variable
> matches an element in the List, Tupple, or Set.
>
> E.g.
>
> x = "apple"
>
> x-list = ["apple", "banana", "peach"]
You don't want to compare with "apple" *equals* the list, you want to
compare whether "apple" is contained *in* the list.
if x in x_list:
print('Comparison is True')
If the list is very large, this might be slow. You should use a set instead
of a list. In Python 3, change the square brackets to curly ones:
{"apple", "banana", "peach"}
In Python 2, you have to pass the list to the set() function:
set(["apple", "banana", "peach"])
For just three items, there's little difference in performance, but if you
find yourself with (say) three hundred items, the set version will be much
faster.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-14 23:30 -0700 |
| Message-ID | <b5cae758-d246-4fdb-b5ce-0961d9d409c6@googlegroups.com> |
| In reply to | #104883 |
On Tuesday, March 15, 2016 at 5:56:54 AM UTC+5:30, jj0ge...@gmail.com wrote:
> In Python is it possible to comparison-equate a variable to a List, Tupple, or Set and have it return True if the contents of the variable matches an element in the List, Tupple, or Set.
>
> E.g.
>
> x = "apple"
>
> x-list = ["apple", "banana", "peach"]
>
> If x == x-list:
> print('Comparison is True')
> else:
> print('Comparison is False')
Others have answered some parts
>>> if x in x_list:
... print("That is a fruit.")
... else:
... print("That is not a fruit.")
...
However one can distribute the print out of the if; Thus
print ("This is a fruit" if x in x_list else "This is not a fruit")
Which can be further distributed:
print "This is %s a fruit" % ("" if x in x_list else "not")
And once you do that you may see that mostly you dont want the print at all.
>>> "This is %s a fruit" % ("" if x in x_list else "not")
See expression oriented thinking here:
http://blog.languager.org/2012/10/functional-programming-lost-booty.html
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-03-15 09:29 +0100 |
| Subject | Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. |
| Message-ID | <mailman.156.1458030609.12893.python-list@python.org> |
| In reply to | #104922 |
Rustom Mody wrote:
> Others have answered some parts
>>>> if x in x_list:
> ... print("That is a fruit.")
> ... else:
> ... print("That is not a fruit.")
> ...
>
> However one can distribute the print out of the if; Thus
>
>>>> "This is %s a fruit" % ("" if x in x_list else "not")
Which of the two versions will most readers grasp at first sight?
Which one is easier to modify so that it works for arbitrary attributes?
Which one is easier to internationalize?
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-15 04:08 -0700 |
| Subject | Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. |
| Message-ID | <18e54ae4-0471-4c3f-842c-da67eedeff02@googlegroups.com> |
| In reply to | #104923 |
On Tuesday, March 15, 2016 at 2:00:25 PM UTC+5:30, Peter Otten wrote:
> Rustom Mody wrote:
>
> > Others have answered some parts
> >>>> if x in x_list:
> > ... print("That is a fruit.")
> > ... else:
> > ... print("That is not a fruit.")
> > ...
> >
> > However one can distribute the print out of the if; Thus
> >
> >>>> "This is %s a fruit" % ("" if x in x_list else "not")
>
> Which of the two versions will most readers grasp at first sight?
> Which one is easier to modify so that it works for arbitrary attributes?
> Which one is easier to internationalize?
Heh!
I think you are saying that my (last) version is clever in a rather stupid
sort of way. Yes?
Well if that is what someone recommends for serious programming then guilty
as charged
But there is a world of difference between
- What one SHOULD (or not) do
- What one CAN do
The first is about serious|professional software engineering
The second is about getting an education beyond basic to some more familiarity
I assumed that OP is in the noob stage and was welcome some learning.
So what I wanted to convey is not so much that such expressions are nice to
have in serious code. Rather that
1. Like algebra has laws so does programming
2. That these laws can be used to massage one program into another
3. That expressions (like %-format) can occur elsewhere than in prints
4. That prints are usually unnecessary (and an abomination)
Not that 3 and 4 come out so well as 1,2 in the above example.
However to answer your questions specifically.
Internationalization: Terrible
Arbitrary attributes: not sure what you are referring to
Readability: Very much like beauty -- in the eye of the beholder
Some things are universally beautiful; some only in some cultural contexts
Likewise readability
Partly people find if-expressions unreadable because they are not used to them.
This is backward because expressions are as basic than statements -- if
anything more basic.
It is *symmetric* Unfortunately understood as lopsided
More such symmetries in this table:
http://blog.languager.org/2016/01/primacy.html#expstat
Partly python if-expressions are unreadable because they are backward compared
to if-statements. A minor syntactic nuisance but yes it does impact readability
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-03-15 18:34 +0100 |
| Subject | Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. |
| Message-ID | <mailman.164.1458063313.12893.python-list@python.org> |
| In reply to | #104932 |
Rustom Mody wrote:
> On Tuesday, March 15, 2016 at 2:00:25 PM UTC+5:30, Peter Otten wrote:
>> Rustom Mody wrote:
>>
>> > Others have answered some parts
>> >>>> if x in x_list:
>> > ... print("That is a fruit.")
>> > ... else:
>> > ... print("That is not a fruit.")
>> > ...
>> >
>> > However one can distribute the print out of the if; Thus
>> >
>> >>>> "This is %s a fruit" % ("" if x in x_list else "not")
>>
>> Which of the two versions will most readers grasp at first sight?
>> Which one is easier to modify so that it works for arbitrary attributes?
>> Which one is easier to internationalize?
>
> I think you are saying that my (last) version is clever in a rather stupid
> sort of way. Yes?
Well, yes ;)
> Well if that is what someone recommends for serious programming then
> guilty as charged
>
> But there is a world of difference between
> - What one SHOULD (or not) do
> - What one CAN do
>
> The first is about serious|professional software engineering
> The second is about getting an education beyond basic to some more
> familiarity
>
> I assumed that OP is in the noob stage and was welcome some learning.
> So what I wanted to convey is not so much that such expressions are nice
> to have in serious code. Rather that
> 1. Like algebra has laws so does programming
> 2. That these laws can be used to massage one program into another
> 3. That expressions (like %-format) can occur elsewhere than in prints
That (the % part) is a lesson for C programmers rather than newbies ;)
> 4. That prints are usually unnecessary (and an abomination)
They aren't. They are often misused by beginners when they occur where a
function should return a value.
> Not that 3 and 4 come out so well as 1,2 in the above example.
>
> However to answer your questions specifically.
>
> Internationalization: Terrible
> Arbitrary attributes: not sure what you are referring to
> Readability: Very much like beauty -- in the eye of the beholder
> Some things are universally beautiful; some only in some cultural contexts
> Likewise readability
>
> Partly people find if-expressions unreadable because they are not used to
> them.
> This is backward because expressions are as basic than statements -- if
> anything more basic.
I think the problem is not that you prefer a programming paradigm that is
not Python's default -- it's that you crammed too many ideas into one
example. It's probably most helpful to concentrate on your main point, e. g.
(You can) Use an expression:
>>> x = "flying saucer"
>>> "This is a fruit" if x in x_list else "This is not a fruit"
'This is not a fruit'
> It is *symmetric* Unfortunately understood as lopsided
> More such symmetries in this table:
> http://blog.languager.org/2016/01/primacy.html#expstat
>
> Partly python if-expressions are unreadable because they are backward
> compared to if-statements. A minor syntactic nuisance but yes it does
> impact readability
Indeed. It's still better than
"This is %s a fruit" % (x in x_list and "" or "not")
The bug is intentional; the fix is of course
"This is %s a fruit" % (x in x_list and "most likely" or "probably not")
;)
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-03-15 19:54 -0700 |
| Subject | Re: Readability counts, was Re: Use of Lists, Tupples, or Sets in IF statement. |
| Message-ID | <c7aad0f1-41c3-4f58-9757-e281ff951172@googlegroups.com> |
| In reply to | #104946 |
On Tuesday, March 15, 2016 at 11:05:32 PM UTC+5:30, Peter Otten wrote: > Indeed. It's still better than > > "This is %s a fruit" % (x in x_list and "" or "not") > > The bug is intentional; the fix is of course > > "This is %s a fruit" % (x in x_list and "most likely" or "probably not") > > ;) Thanks I wondered what/why you were bringing in this clunky, error-prone pre-conditional meme... And came across https://mail.python.org/pipermail/python-dev/2005-September/056510.html | I propose that in Py3.0, the "and" and "or" operators be simplified to | always return a Boolean value instead of returning the last evaluated | argument. Good to know that Raymond Hettinger disagrees with Guido on the messed-up, ½-class status of bools in python
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web