我有一个字典,索引号作为键,范围从0到9,字典的值是一个列表,其中索引号0到7包含5个元素,索引号8和9包含6个元素
任何人都可以帮助修改测试目录以实现相同的目标
test_dict = {0: [7110, 7000, 8800, 11000, 15000], 1:[7590, 7000, 8800, 11000, 15000], 2:[7080, 7000, 8800, 11000, 15000], 3:[7620, 7000, 8800, 11000, 15000], 4:[7050, 7000, 8800, 11000, 15000], 5:[7590, 7000, 8800, 11000, 15000], 6:[6990, 7000, 8800, 11000, 15000], 7:[7400, 7000, 8800, 11000, 15000], 8:[2760, 3000, 3800, 4800, 6000, 8500], 9:[2730, 3000, 3800, 4800, 6000, 8500]}
max_lst_len = max(len(lst) for lst in test_dict.items())
for i in range(max_lst_len):
speeds={}
for index in test_dict:
try:
speed = test_dict[index][i]
speeds[index] = speed
except IndexError:
continue
抱歉,如果问题的形式不是最好的。我是新来的。 我期待的预期修改dict是
{0: [8000, 8800, 11000, 15000], 1:[8000, 8800, 11000, 15000],.....,8:[5500, 4800, 6000, 8500], 9:[5500, 4800, 6000, 8500]
提供的解决方案中断,不考虑此请求:
这是:
如果您确定输入大小为10,我们可以进一步简化为:
还要注意,您的示例输出并没有忽略前两个元素,而是忽略了一个元素,所以我就是这么做的。如果我们忽略您提到的前两项:
如果您确定输入大小为10,我们可以进一步简化为:
不确定这是否适用于您,因为结果与发布的预期结果不匹配
但是,发布的预期结果与文章中的描述不匹配-它似乎忽略了最后两个列表中的第一个项目,而不是前两个项目
结果
这些是我正在使用的规格
test_dict
总而言之:
这段代码非常分散,很容易适应您的需要
干杯
相关问题 更多 >
编程相关推荐