I need help on how to iterate over rows. For example, let's say I have a variable
a = 30
What I would like to do is using a loop in order to obtain something like that:
0,0,0,0,0,0,0,0,.....,0 (30 zeros, because a=30)
1,1,1,1,1,1,1,1,....,1 (29 ones)
2,2,2,2,2,2,2,2,...,2 (28 twos)
.....
30 (1 thirty)
I tried something like this:
L = [0]
for x in L:
while L[x] < a:
print(x)
L[0] = L[0] 1
But doing this I obtain only 30 zeros. I'm not able to increase L to restart the count.
CodePudding user response:
Here is an example of how you can use a nested loop to achieve the desired outcome:
for i in range(0, 31):
for j in range(30-i):
print(i, end=',')
print()
This code will output the following:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
CodePudding user response:
a = 30
b = 30
for i in range(a, 0, -1):
for j in range(i 1):
print(b-i, end=',')
print()
print(30)
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
17,17,17,17,17,17,17,17,17,17,17,17,17,17,
18,18,18,18,18,18,18,18,18,18,18,18,18,
19,19,19,19,19,19,19,19,19,19,19,19,
20,20,20,20,20,20,20,20,20,20,20,
21,21,21,21,21,21,21,21,21,21,
22,22,22,22,22,22,22,22,22,
23,23,23,23,23,23,23,23,
24,24,24,24,24,24,24,
25,25,25,25,25,25,
26,26,26,26,26,
27,27,27,27,
28,28,28,
29,29,
30
CodePudding user response:
the suggestion below needs numpy
import numpy as np
a=30
token=0
for i in range(a,0,-1):
x=np.repeat(token,i)
print(x)
token=token 1
CodePudding user response:
Instead of using nested loops and reassigning into a temporary list, use the following short approach based on inversed range
and string multiplication:
a = 30
for fill_v, k in enumerate(range(a 1, 0, -1)):
print((f'{str(fill_v)},' * k).rstrip(','))
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
17,17,17,17,17,17,17,17,17,17,17,17,17,17
18,18,18,18,18,18,18,18,18,18,18,18,18
19,19,19,19,19,19,19,19,19,19,19,19
20,20,20,20,20,20,20,20,20,20,20
21,21,21,21,21,21,21,21,21,21
22,22,22,22,22,22,22,22,22
23,23,23,23,23,23,23,23
24,24,24,24,24,24,24
25,25,25,25,25,25
26,26,26,26,26
27,27,27,27
28,28,28
29,29
30
CodePudding user response:
If you start with 30 0
s, you should end with 0 30
s [i.e., at 1 29
],
# for i in range(a): print(','.join([f'{i}']*(a-i))) # OR
for i in range(a): print(*[i]*(a-i), sep=',')
You can end at 1 30
if you start with 31 0
s [ output screenshot ]
for i in range(a 1): print(*[i]*(a-i 1), sep=',')
If you to save it as an object
{f"{(a-i)} {i}'s": tuple([i]*(a-i)) for i in range(a)}
would return a dictionary of tuples
{
"30 0's": (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
"29 1's": (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
"28 2's": (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
"27 3's": (3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3),
"26 4's": (4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4),
"25 5's": (5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5),
"24 6's": (6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6),
"23 7's": (7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7),
"22 8's": (8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8),
"21 9's": (9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9),
"20 10's": (10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10),
"19 11's": (11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11),
"18 12's": (12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12),
"17 13's": (13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13),
"16 14's": (14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14),
"15 15's": (15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15),
"14 16's": (16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16),
"13 17's": (17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17),
"12 18's": (18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18),
"11 19's": (19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19),
"10 20's": (20, 20, 20, 20, 20, 20, 20, 20, 20, 20),
"9 21's": (21, 21, 21, 21, 21, 21, 21, 21, 21),
"8 22's": (22, 22, 22, 22, 22, 22, 22, 22),
"7 23's": (23, 23, 23, 23, 23, 23, 23),
"6 24's": (24, 24, 24, 24, 24, 24),
"5 25's": (25, 25, 25, 25, 25),
"4 26's": (26, 26, 26, 26),
"3 27's": (27, 27, 27),
"2 28's": (28, 28),
"1 29's": (29)
}
CodePudding user response:
var = 30
count = 0
for i in range(var 1, 0, -1):
for j in range(0,i):
print(count, end='', sep='')
print("\n")
count = count 1
The code defines two variables var with a value of 30, and count with a value of 0. It then uses a nested for loop to iterate over a range of numbers, starting from var 1 and ending at 0 in descending order. On each iteration of the inner loop, the code prints the current value of the count variable, followed by a newline character. The value of count is incremented by 1 on each iteration of the outer loop. The output is a series of rows, each row containing a different number of the same value, starting from 0 and ending with 30. The number of times each number is printed in each row will be decremented by 1 starting from 30 to 1.
CodePudding user response:
That can be achieved with a simple nested loop as follows:
a = 30
for i in range(a 1):
for j in range(1,a-i 2):
if j == a-i 1:
print(i,end='')
else:
print(i,end=',')
print()
Notice the if condition to not put ',' at the end as on your example.
The output:
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
17,17,17,17,17,17,17,17,17,17,17,17,17,17
18,18,18,18,18,18,18,18,18,18,18,18,18
19,19,19,19,19,19,19,19,19,19,19,19
20,20,20,20,20,20,20,20,20,20,20
21,21,21,21,21,21,21,21,21,21
22,22,22,22,22,22,22,22,22
23,23,23,23,23,23,23,23
24,24,24,24,24,24,24
25,25,25,25,25,25
26,26,26,26,26
27,27,27,27
28,28,28
29,29
30