Home > database >  Using dict values to count iterations over a list to insert
Using dict values to count iterations over a list to insert

Time:10-22

import random
from itertools import repeat

races_per_season = {
    '2015' : "19",
    '2016' : "21",
    '20116' : "21",
    '2017' : "20",
    '2018' : "21",
    '2019' : "21",
    '2020' : "17",
    '2021' : "16"
}

tmp_list = list(repeat(random.sample(range(80),10), 156))
total_races = 0
for k,v in races_per_season.items(): 
    while total_races < int(v):
        tmp_list[total_races].insert(1, k)
        total_races  = 1
        break # inserting breaks here and below, somewhat works, but only gives me the first year throughout the list
    break


for x in tmp_list:
    print(x)

I am trying to use the dict values to iterate over a list of list and insert the key into the list at index 1. However, no matter how I try, it seems to iterate and insert all keys into the list then moves on to the next...

This is the result I am seeing.... however by adding the breaks above, this continues throughout the list of 156 lists.. and doesn't change at list 19

[[29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015',  56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015',  56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73]]

but my desired result is the following.

[[29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73],
 [29, '2015', 56, 39, 31, 25, 37, 5, 16, 8, 73]...

and continuing with '2015' 19 times, then inserting '2016' into the following 21 etc.. When I just print out the k,v pairs it works as I want it to, but I can't somehow convert that into a list. All values in the dict sum to the value of the len(tmp_list) 156

ANY help would be more than appreciated. Thanks

CodePudding user response:

Check this

import random
from itertools import repeat

races_per_season = {
    '2015' : "19",
    '2016' : "21",
    '20116' : "21",
    '2017' : "20",
    '2018' : "21",
    '2019' : "21",
    '2020' : "17",
    '2021' : "16"
}
# this line to create 156 lists that are not sharing the same reference
tmp_list = [list(arr) for arr in repeat(random.sample(range(80),10), 156)]
i = 0
for k,v in races_per_season.items():
    total_races = 0
    while total_races < int(v):
        tmp_list[i].insert(0, k)
        tmp_list[i].insert(0, int(v))
        total_races  = 1
        i =1

for x in tmp_list:
    print(x)

CodePudding user response:

You can try this, seems to be working for me at least.

import random
from itertools import repeat

races_per_season = {
    '2015' : "19",
    '2016' : "21",
    '20116' : "21",
    '2017' : "20",
    '2018' : "21",
    '2019' : "21",
    '2020' : "17",
    '2021' : "16"
}

tmp_list = list(repeat(random.sample(range(80), 10), 156))

tmp_list = [[tmp_list[i][0], k, *tmp_list[i][1:]]
            for k, v in races_per_season.items()
            for i in range(int(v))]

for x in tmp_list:
    print(x)

Result:

[44, '2015', 18, 33, 30, 54, 22, 53, 19, 4, 68]
...repeated 18 times
[44, '2016', 18, 33, 30, 54, 22, 53, 19, 4, 68]
...repeated 20 times
...

Explanation

Above approach (also copied below) uses two list comprehensions. The first one iterates over key-value pairs in the dictionary, and the second one iterates over all numbers 0...n-1 in the value for each key - for example, with key '2015' we have n=19.

[[tmp_list[i][0], k, *tmp_list[i][1:]]
            for k, v in races_per_season.items()
            for i in range(int(v))]

To explain the following syntax used:

[tmp_list[i][0], k, *tmp_list[i][1:]]

This basically says:

  1. create a new list with the first element in tmp_list[i]
  2. add the key we're iterating over as the second element
  3. The [1:] says, get me all the elements in a list except for the first element. The star * operator unpacks that result again, so we don't end up with a list within each list for example.

CodePudding user response:

You can first create a separate list for years, then add year to each list

years = [k  for k, v in races_per_season.items() for i in range(int(v))]

res = [[years[idx]]   lst for idx, lst in enumerate(tmp_list)]
print(res)
  • Related