[Tutor] Multiple for and if/else statements into a single list comprehension

2014-03-17 Thread Jignesh Sutar
Is it possible to get two nested for statements followed by a nested
if/else statement all into a single list comprehension ie. the equivalent
of the below:


for i in xrange(1,20):
for j in xrange(1,10):
if j6:
j=int(8+str(j))
else:
j=int(9+str(j))
print %(i)02d_%(j)02d % locals()


# double for statement without if/else works
print \n.join([%(i)02d_%(j)02d % locals() for i in xrange(1,20) for j
in xrange(1,10)])

#now try to incorporate if/else part
#failed attempt 1
print \n.join([%(i)02d_%(j)02d % locals() for i in xrange(1,20) for j
in xrange(1,10) j=int(8+str(j)) if j6 else int(9+str(j))])

#failed attempt 2
print \n.join([%(i)02d_%(j)02d % locals() for i in xrange(1,20)
j=int(8+str(j)) if j6 else int(9+str(j)) for j in xrange(1,10)])

#failed attempt 3
print \n.join([%(i)02d_%(j)02d % locals() j=int(8+str(j)) if j6 else
int(9+str(j)) for i in xrange(1,20)  for j in xrange(1,10)])


Many thanks in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple for and if/else statements into a single list comprehension

2014-03-17 Thread Peter Otten
Jignesh Sutar wrote:

 Is it possible to get two nested for statements followed by a nested
 if/else statement all into a single list comprehension ie. the equivalent
 of the below:
 
 
 for i in xrange(1,20):
 for j in xrange(1,10):
 if j6:
 j=int(8+str(j))
 else:
 j=int(9+str(j))
 print %(i)02d_%(j)02d % locals()

 Many thanks in advance.

Need I say that it is a bad idea to build overly complex list 
comprehensions? Under that proviso:

The first step is always to ensure that there is a single expression in the 
inner loop. I'm keeping as similar as possible to your loops:

for i in xrange(1, 20):
for j in xrange(1, 10):
print %02d_%02d % (i,
int((8 if j  6 else 9) + str(j)))

Now the translation is mechanical

for x in a:
for y in b:
expr

becomes

[expr for x in a for y in b]

or (using a genexp rather than a listcomp as you are going to print it 
anyway)

print \n.join(
%02d_%02d % (i, int((8 if j  6 else 9) + str(j)))
for i in xrange(1, 20)
for j in xrange(1, 10))

But again: don't do that ;)

By the way, using locals() is another bad idea, plus it does not capture the 
loop vars of genexps (all pythons) and listcomps (python3).

PS: a simple alternative:

print \n.join(
%02d_%02d % (i, k) 
for i in range(1, 20) 
for k  in range(81, 86) + range(96, 100))


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple for and if/else statements into a single list comprehension

2014-03-17 Thread Peter Otten
Peter Otten wrote:

  [locals()] does not capture
 the loop vars of genexps (all pythons) and listcomps (python3).

Sorry, I was totally wrong on that one.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple for and if/else statements into a single list comprehension

2014-03-17 Thread spir

On 03/17/2014 11:22 AM, Jignesh Sutar wrote:

Is it possible to get two nested for statements followed by a nested
if/else statement all into a single list comprehension ie. the equivalent
of the below:


for i in xrange(1,20):
 for j in xrange(1,10):
 if j6:
 j=int(8+str(j))
 else:
 j=int(9+str(j))
 print %(i)02d_%(j)02d % locals()


You can do it by reformulating your inner block into an expression (here, using 
a ternary if expression), which will then become the expression part of the 
comprehension. However, a few remarks:


* don't do that: the only advantage is to make your code unreadable
* you may reformulate using 2 comprehensions; if you don't want intermediate 
lists, use a generator expression for the inner one
* above, the inner j is a new variable with a distinct meaning: why do you call 
it j?

* do you really need string concat to perform arithmetic?

d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Multiple for and if/else statements into a single list comprehension

2014-03-17 Thread Jignesh Sutar
Is it possible to get two nested for statements followed by a nested
if/else statement all into a single list comprehension ie. the equivalent
of the below:


for i in xrange(1,20):
for j in xrange(1,10):
if j6:
j=int(8+str(j))
else:
j=int(9+str(j))
print %(i)02d_%(j)02d % locals()


# double for statement without if/else works
print \n.join([%(i)02d_%(j)02d % locals() for i in xrange(1,20) for j
in xrange(1,10)])

#now try to incorporate if/else part
#failed attempt 1
print \n.join([%(i)02d_%(j)02d % locals() for i in xrange(1,20) for j
in xrange(1,10) j=int(8+str(j)) if j6 else int(9+str(j))])

#failed attempt 2
print \n.join([%(i)02d_%(j)02d % locals() for i in xrange(1,20)
j=int(8+str(j)) if j6 else int(9+str(j)) for j in xrange(1,10)])

#failed attempt 3
print \n.join([%(i)02d_%(j)02d % locals() j=int(8+str(j)) if j6 else
int(9+str(j)) for i in xrange(1,20)  for j in xrange(1,10)])


Many thanks in advance.
Jignesh
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 121, Issue 42 nested for

2014-03-17 Thread fabu desay
python executes the first for and its condititions then the next for.
What i'm saying is that you should first deal with one  as nested block codes
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple for and if/else statements into a single list comprehension

2014-03-17 Thread Jignesh Sutar
Thanks Peter/Denis. I wasn't aware of genexp. I see how you have adapted
the code to make it work, I'll adapt the same in my program. Good point
about duplicating j , Denis, I guess I was happy to override the outer j as
it was intermediate.


On 17 March 2014 12:36, spir denis.s...@gmail.com wrote:

 On 03/17/2014 11:22 AM, Jignesh Sutar wrote:

 Is it possible to get two nested for statements followed by a nested
 if/else statement all into a single list comprehension ie. the equivalent
 of the below:


 for i in xrange(1,20):
  for j in xrange(1,10):
  if j6:
  j=int(8+str(j))
  else:
  j=int(9+str(j))
  print %(i)02d_%(j)02d % locals()


 You can do it by reformulating your inner block into an expression (here,
 using a ternary if expression), which will then become the expression part
 of the comprehension. However, a few remarks:

 * don't do that: the only advantage is to make your code unreadable
 * you may reformulate using 2 comprehensions; if you don't want
 intermediate lists, use a generator expression for the inner one
 * above, the inner j is a new variable with a distinct meaning: why do you
 call it j?
 * do you really need string concat to perform arithmetic?


 d
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 https://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor