I have
for i in range(0, 11): print i, "\n", i
I want my python program to print this path for every loop cycle
1st cycle:
1 1
Second cycle:
1 2 2 1
Third cycle:
1 2 3 3 2 1
I tried using \ r \ n or \ 033 [1A, but they just overwrite the previous line. Is there a way I can βpopβ the output string so that I don't overwrite it?
One way to do this,
def foo(x, limit): if x < limit : print x foo(x + 1, limit) print x foo(1, 11)
This cannot be done in 1 for the loop as you are trying now.
As I suggested, you can do this using these lists
>>> l1 = [] >>> l2 = [] >>> for i in range(0, 11): ... l1.append(i) ... l2 = [i] + l2 >>> l1.extend(l2) >>> for i in l1: ... print i 0 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 0
.
>>> ladder = [x for x in range(5)] + [x for x in range(5,-1,-1)] >>> ladder [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0] >>> for x in ladder: ... print x ... 0 1 2 3 4 5 4 3 2 1 0 >>>
def two_side_print(start, end): text = [str(i) for i in range(start,end)] print '\n'.join(text), '\n', '\n'.join(reversed(text)) two_side_print(1, 11)
Another option is to save the printed values ββin a list and print this list in reverse order at each iteration of the loop.
My suggestion:
l = [] for i in range(1, 5): print 'Loop '+str(i) # This is to ease seeing the printing results for val in l: print val print i l.insert(len(l),i) for val in reversed(l): print val
Output for a loop repeating from 0to 5:
0
5
Loop 1 1 1 Loop 2 1 2 2 1 Loop 3 1 2 3 3 2 1 Loop 4 1 2 3 4 4 3 2 1
Hope this is what you are looking for.
You can use the recursive function to help here:
def print_both_ways(start, end): print(start) if start != end: print_both_ways(start+1, end) print(start)
Using:
print_both_ways(1,3) # prints 1,2,3,3,2,1 on separate lines
Source: https://habr.com/ru/post/1606259/More articles:An effective way to apply the mirror effect when turning a quaternion? - mathWhat is the internal behavior of exception handling? - javaSetting colors in draw_networkx and in matplotlib - pythonWarning Implicit declaration posix_memalign - cSUM dynamic columns in a PIVOT table in SQL Server - sql-serverjQuery Datatables: search and filter by Ajax pagination - jqueryMemory exception exception during recursive programming on a Java map - javaTypeError: type () takes 1 or 3 arguments - pythonhow to convert spring security configuration from .groovy application to application.yml - spring-securityhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1606264/how-to-pass-parameters-to-activemodelarrayserializer&usg=ALkJrhiIAl_BFNde6awnTajW-sPWEhRJxgAll Articles