Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: pythonic way

72 views
Skip to first unread message

MRAB

unread,
Nov 1, 2012, 11:52:11 AM11/1/12
to pytho...@python.org
On 2012-11-01 15:32, inshu chauhan wrote:
> what is the most pythonic way to do this :
>
> if 0 < ix < 10 and 0 < iy < 10 ???
>
>
That looks Pythonic to me, except for the missing colon.

Tim Chase

unread,
Nov 1, 2012, 11:57:12 AM11/1/12
to inshu chauhan, pytho...@python.org
On 11/01/12 10:32, inshu chauhan wrote:
> what is the most pythonic way to do this :
>
> if 0 < ix < 10 and 0 < iy < 10 ???

What's wrong with the syntax you provide? It's perfectly pythonic:

ix = 42
yx = 3.14159
if 0 < ix < 10 and 0 < iy < 10:
do_stuff(ix, iy)
else:
do_other_stuff(ix, iy)

-tkc



Terry Reedy

unread,
Nov 1, 2012, 12:04:12 PM11/1/12
to pytho...@python.org
On 11/1/2012 11:32 AM, inshu chauhan wrote:
> what is the most pythonic way to do this :
>
> if 0 < ix < 10 and 0 < iy < 10 ???

end with : instead of ???

>>> ix, iy = 3,4
>>> if 0 < ix < 10 and 0 < iy < 10:
print('This was too easy')


This was too easy

--
Terry Jan Reedy

Message has been deleted

Zero Piraeus

unread,
Nov 1, 2012, 12:54:43 PM11/1/12
to pytho...@python.org
:

On 1 November 2012 11:32, inshu chauhan <insid...@gmail.com> wrote:
> what is the most pythonic way to do this :
>
> if 0 < ix < 10 and 0 < iy < 10 ???

As everyone else has said, it's perfectly pythonic once you stick the
colon on the end. You might find it more instantly readable with some
extra parentheses:

if (0 < ix < 10) and (0 < iy < 10):
# do something

... but that's really just down to taste.

-[]z.

jack

unread,
Nov 2, 2012, 3:14:32 AM11/2/12
to pytho...@python.org
Sometimes, I need to alter the element as traverse a list like this
(it's a sample):
c = range(10)
i = 0
for ele in c:
# do something
# branch:
c[i] = # value
i += 1

How to be pythonic?

2012/11/2 0:54, Zero Piraeus :

Chris Angelico

unread,
Nov 2, 2012, 3:56:33 AM11/2/12
to pytho...@python.org
On Fri, Nov 2, 2012 at 6:14 PM, jack <naru...@live.cn> wrote:
> Sometimes, I need to alter the element as traverse a list like this (it's a
> sample):
> c = range(10)
> i = 0
> for ele in c:
> # do something
> # branch:
> c[i] = # value
> i += 1
>
> How to be pythonic?

Check out the enumerate() function.

ChrisA

jack

unread,
Nov 2, 2012, 4:58:17 AM11/2/12
to Chris Angelico, pytho...@python.org
thanks,but I don't think enumerate() is my want
Have some ways to operate the reference of element,not a copy when I
tried to traverse a list?

I'm so sorry about my poor English, hope you don't mind it.

Chris Angelico

unread,
Nov 2, 2012, 5:11:49 AM11/2/12
to pytho...@python.org
On Fri, Nov 2, 2012 at 7:58 PM, jack <naru...@live.cn> wrote:
> thanks,but I don't think enumerate() is my want
> Have some ways to operate the reference of element,not a copy when I tried
> to traverse a list?
>
> I'm so sorry about my poor English, hope you don't mind it.

No probs, I'll be a little less vague and pointer-y and give you some
example code. :)

lst = ['foo', 'bar', 'quux', 'asdf', 'qwer', 'zxcv']
for idx, val in enumerate(lst):
if val[1]=='w': lst[idx]='Replaced'
print(lst)

['foo', 'bar', 'quux', 'asdf', 'Replaced', 'zxcv']


Does that explain it a bit better? You get the index and can then
mutate the list using that index, thus replacing the original entry.

ChrisA
0 new messages