python - Using dict values to count iterations over a list to insert
问题描述
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
解决方案
你可以试试这个,似乎至少对我有用。
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))
seasons = [season for season, races in races_per_season.items()
for i in range(int(races))]
tmp_list = [[L[0], seasons[i], *L[1:]] for i, L in enumerate(tmp_list)]
for x in tmp_list:
print(x)
结果:
[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
...
解释
在上述方法的第一部分,我们使用两个list
推导来构建一个years
变量。第一个推导遍历字典中的键值对,第二个推导遍历0...n-1
每个键的值中的所有数字——例如,我们有键 '2015' n=19
。
解释以下使用的语法:
[L[0], seasons[i], *L[1:]]
这基本上说:
- 创建一个新列表,其中的第一个元素
L
是 中的子列表tmp_list
- 添加我们正在迭代的季节作为第二个元素
[1:]
说,给我除了第一个元素之外的子列表中的所有元素。星*
号运算符再次解包该结果,因此我们不会在每个列表中都有一个列表。
推荐阅读
- java - 在 Kafka Streams 中序列化 HashMap
- python - 图像过滤/分类/识别
- google-cloud-firestore - Google Cloud Function - 存储触发器抱怨返回未定义
- mongodb - mongodb 忽略 dbPath 参数?
- matlab - 在 Matlab 中查找表中上一行的索引
- kubernetes - 防止 OpenShift/Kubernetes 上的 RabbitMQ 出现裂脑/分区
- python - Python:无法修复“ModuleNotFoundError:没有名为‘sklearn’的模块”
- django - Django图像标签不显示照片
- jquery - Get data from all rows of a datatable
- sql - UNION ALL 将缺失的日期添加到表中