views:

122

answers:

5
num = list(str(1234567))

for n1 in num:
    print n1
    for n2 in reversed(num):
        print '\t', n2

On each iteration, it prints the first digit from the first loop and all 7 from the reverse loop. How can I print not all digits but only the last (i.e first) digit from reverse loop?

Thanks

+1  A: 

Here's a feeble attempt. Is this the kind of thing you're looking for?

 for idx,i in enumerate(x):
     print i,"\t",x[-(idx+1)]
Noufal Ibrahim
Oh, whats "idx" for? *looks up the manual*
Nimbuz
`enumerate` returns a list of tuples each element of which is the index (which I often abbreviate to idx) and the element. `enumerate("abc") => [(0,'a'),(1,'b'),(2,'c')]`
Noufal Ibrahim
Oh great, nice to know about that, Thanks!
Nimbuz
+1  A: 

Do you mean like this?

num = list(str(1234567))
for i in range(len(num)):
    print num[i], '\t', num[-(i+1)]

Output is:

1       7                       
2       6                       
3       5                       
4       4                       
5       3                       
6       2                       
7       1  
ezod
What does "num[-(i+1)]" do? and why not just "for i in num"?
Nimbuz
`for i in num` iterates over num (touches every element in `num`). `num[-(i+1)]` is lookup of a specific element by index.
Noufal Ibrahim
The negative index means to start from the end of the list. The `i+1` is necessary because, for example, `num[-1]` is the last item in the list, yet the forward indexing (and thus, the range of values of `i`) begins with 0.Using `for i in num` will make `i` equal to the value of one of the items in `num` at each iteration, as opposed to the list index. `range(len(num))` by contrast returns (basically) a list of indices for `num`.
ezod
Great, learnt a lot of new things from this one simple example, thanks all! :)
Nimbuz
+7  A: 

Simplest way is to just zip the forward and reverse lists together:

for n1, n2 in izip(num, reversed(num)):
    print n1, '\t', n2
wich
Neat. I like this.
Noufal Ibrahim
izip = zip and %d = %s here. Yea, great solution.
Nimbuz
Need `from itertools import izip`, but this also works with just `zip()`.
ezod
I have to use Java and I think what you did was sufficiently advanced technology.
Robert Grant
@Nimbuz, as ezod said, izip is from itertools. The difference is that it's more memory efficient if you have large lists. It doesn't create the whole list in one go, but instead generates it an element at a time.
wich
Ok great, I didn't even know it existed, I thought it was a typo. :P
Nimbuz
A: 

You should make a second list:

>>> num_rev = num[:]
>>> num_rev.reverse()
>>> num_rev
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Then do something like:

>>> for n1,n2 in zip(num,num_rev): 
...     print n1, n2
... 
0 9
1 8
2 7
3 6
4 5
5 4
6 3
7 2
8 1
9 0
telliott99
A: 

Nothing to do with Python, but here it is in Haskell :)

myDie  = [1,2,3,4,5,6]
sevens = [ (x,y) | x <- myDie, y <- myDie, x+y == 7]
Robert Grant
Okay, but why weird variable names? :)
Nimbuz