2

This is probably a simple problem, but I am trying to created a nested loop that would count up from 0 to 9 in the outer loop, and in the inner loop, start from the value (or index. They are the same in this case) of the outer loop and count backwards.

Here's an example:

i= 0
k= 0

i= 1
k= 1
k= 0 

i= 2
k= 2
k= 1
k= 0

i= 3
k= 3
k= 2
k= 1
k= 0

I got this far:

x = range(0,10)
for i in x:
    print 'i = ',x[i]
    for k in x:
        print 'k = ', x[i::-1]

Obviously, the code above doesn't do what I want it to do. For one, the second for loop doesn't start from the value of i in the outer loop and counts backwards. For another, it doesn't print a new k = for every new value.

2
  • I assume here that k[i::-1] is really x[i::-1], isn't it? Commented Feb 4, 2015 at 23:46
  • Or you can do range( i, -1, -1 ) to get [ i, i-1, i-2, ...., 2, 1, 0] Commented Feb 4, 2015 at 23:51

3 Answers 3

2

I think this should be like this:

x = range(0,10)
for i in x:
    print 'i = ',x[i]      
    for k in x[i::-1]:
        print 'k = ', k
    print("\n")

The result is:

i =  0
k =  0


i =  1
k =  1
k =  0


i =  2
k =  2
k =  1
k =  0


i =  3
k =  3
k =  2
k =  1
k =  0


i =  4
k =  4
k =  3
k =  2
k =  1
k =  0


i =  5
k =  5
k =  4
k =  3
k =  2
k =  1
k =  0


i =  6
k =  6
k =  5
k =  4
k =  3
k =  2
k =  1
k =  0


i =  7
k =  7
k =  6
k =  5
k =  4
k =  3
k =  2
k =  1
k =  0


i =  8
k =  8
k =  7
k =  6
k =  5
k =  4
k =  3
k =  2
k =  1
k =  0


i =  9
k =  9
k =  8
k =  7
k =  6
k =  5
k =  4
k =  3
k =  2
k =  1
k =  0

Basicly, x[i::-1] should be in the for not in the print.

Sign up to request clarification or add additional context in comments.

4 Comments

And x[i] > i, as you're there
@RicardoCárdenes What do you mean?
Sorry, I don't understand your comment.
x is a range of integers. In this case there's no sense in using x[i], because i... is x[i]! Same as you're printing k straight, instead of x[k]
2

What about just manipulate it with print function?

i = 0
k = 0

while True:
    print (i)
    print (k)
    if 1<k: #tricky part
        print ("\n".join([str(h) for h in range(0,k+1)][::-1]))
    print ("")
    i += 1
    k += 1
    if i == 10:
        break

Comments

0

You are very close. If you are new to the world of python you can take some inspiration from this example where I use list comprehension.

list = lambda k: [ [ i for i in reversed(xrange(j+1)) ] for j in xrange(k+1) ]

Note: If you are using python 3 "xrange" is changed to "range"

Now call:

list(3)

And you'll see that the result is similar to what you are looking for.

[[0], [1, 0], [2, 1, 0], [3, 2, 1, 0]]

1 Comment

The asker wants to print the results, not put in a list. But nice answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.