lundi 29 mars 2021

Create a new list enumerating the random integers from another list in Python

I need to simulate a certain scenario.

So I'm defining a variable wich generates a loop of a random number of integers.

I get for example:

list = [2, 35, 4, 8, 56, 10]

Then, I'm generating this random list 50 times through another loop and I store the data into a dictionary to visualize a Pandas Dataframe.

data_dict = {'random_numers': list}
data_dict_pd = pd.DataFrame(data_dict)

So I get for example this:

[1, 16, 6, 6, 1, 10]
[3, 8, 4, 4, 1, 20, 7, 25, 12]
[14, 8, 16, 4, 11, 18, 5, 15, 24, 2, 15, 5]
[7, 24, 1, 14]
[5, 14, 19, 24, 1]
... 50 times. 

Now, I need to create another column enumerating each numerber in each list of elements, to get the following, based on the previous results:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
...50 times. 

Actually, came up with the following but it's wrong:

new_list = []
 for index in enumerate(list)
    new_list.append(index)

Any better idea?




Aucun commentaire:

Enregistrer un commentaire