Home > Blockchain >  Create a loop to count 0-6 for 100 time
Create a loop to count 0-6 for 100 time

Time:10-25

Im trying to make a loop to fill a list with values ranging from 0 to 6 for 100 times. i tired it like this but i seems like it it multiply it self 100*6

ls = []
x = 0
i = 0
y = 0

while x != 10:
    for i in range (1,7):
        ls.append(i)
        x  =1
 
print(ls)

Does anybody knows any solution to this. Thanks in advance. If anyone also knows a solution in pandas that would also be great!!

CodePudding user response:

You don't need a loop:

N = 6
lst = list(range(N 1))*100

With /:

import numpy as np
import pandas as pd

N = 6

a = np.tile(np.arange(N 1), 100)

s = pd.Series(a)

CodePudding user response:

You can use range.

>>> list(range(7))*100

Or you can use numpy.tile.

>>> import numpy as np
>>> np.tile(np.arange(7), 100)

CodePudding user response:

Here is one way to do this using Pandas:

import pandas as pd

df = pd.DataFrame([0,1,2,3,4,5,6] * 100)

CodePudding user response:

ls = list(range(1, 7)) * 100

yields

[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6...

for a total length of 600 items.

If you want 100 items, generate a bit less, and cut the list:

ls = (list(range(1, 7)) * (100 // 6   1))[:100]

CodePudding user response:

What about something like this:

LIST_TO_REPEAT = [1,2,3,4,5,6]
REPEAT_AMOUNT = 100

array = []

for i in range(0, REPEAT_AMOUNT):
    array = array   LIST_TO_REPEAT
    
print(array)

This adds the list to LIST_TO_REPEAT to array REPEAT_AMOUNT amount of times

CodePudding user response:

ls = [i for _ in range(100) for i in range(0, 7)]
print(ls)
  • Related