如何操作字典中的列表并添加或修改值以创建新字典?

2024-09-27 04:28:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典,索引号作为键,范围从0到9,字典的值是一个列表,其中索引号0到7包含5个元素,索引号8和9包含6个元素

  • 我要做的是忽略从0到7的每个列表的第0个元素,忽略索引8和9的前两个元素(如果它们存在的话)
  • 将1000添加到索引0到7的已修改列表的第一个元素中,并将2500添加到索引8和9的列表的第一个元素中,然后修改字典并使用该字典在字典中进行迭代并创建字典,如下例所示

任何人都可以帮助修改测试目录以实现相同的目标

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]

Tags: intest目录元素目标列表forindex
3条回答

提供的解决方案中断,不考虑此请求:

(if they exist)

这是:

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]}

for idx, list_ in test_dict.items():
    if 0 <= idx <= 9:
        test_dict[idx] = list_[1:]
        try:
            for i in range(1 if idx <= 7 else 2):
                test_dict[idx][i] += 1000 if idx <= 7 else 2500
        except IndexError:
            pass

print(test_dict)

如果您确定输入大小为10,我们可以进一步简化为:

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]}

for idx, list_ in test_dict.items():
    test_dict[idx] = list_[1:]
    try:
        for i in range(1 if idx <= 7 else 2):
            test_dict[idx][i] += 1000 if idx <= 7 else 2500
    except IndexError:
        pass

print(test_dict)

还要注意,您的示例输出并没有忽略前两个元素,而是忽略了一个元素,所以我就是这么做的。如果我们忽略您提到的前两项:

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]}

for idx, list_ in test_dict.items():
    if 0 <= idx <= 9:
        test_dict[idx] = list_[1 if idx <= 7 else 2:]
        try:
            for i in range(1 if idx <= 7 else 2):
                test_dict[idx][i] += 1000 if idx <= 7 else 2500
        except IndexError:
            pass

print(test_dict)

如果您确定输入大小为10,我们可以进一步简化为:

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]}

for idx, list_ in test_dict.items():
    test_dict[idx] = list_[1 if idx <= 7 else 2:]
    try:
        for i in range(1 if idx <= 7 else 2):
            test_dict[idx][i] += 1000 if idx <= 7 else 2500
    except IndexError:
        pass

print(test_dict)

不确定这是否适用于您,因为结果与发布的预期结果不匹配

但是,发布的预期结果与文章中的描述不匹配-它似乎忽略了最后两个列表中的第一个项目,而不是前两个项目

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]}

speeds = {}

for ky in test_dict.keys():
    speeds[ky] = test_dict[ky][1:] if len(test_dict[ky]) == 5 else test_dict[ky][2:]
    speeds[ky][0] += 1000 if len(test_dict[ky]) == 5 else 2500
    
print(speeds)

结果

{0: [8000, 8800, 11000, 15000], 
 1: [8000, 8800, 11000, 15000], 
 2: [8000, 8800, 11000, 15000], 
 3: [8000, 8800, 11000, 15000], 
 4: [8000, 8800, 11000, 15000], 
 5: [8000, 8800, 11000, 15000], 
 6: [8000, 8800, 11000, 15000], 
 7: [8000, 8800, 11000, 15000], 
 8: [6300, 4800, 6000, 8500], 
 9: [6300, 4800, 6000, 8500]}

这些是我正在使用的规格

  • 忽略从0到7的每个列表的第0个元素
  • 忽略列表8和9的前两个元素
  • 将1000添加到0-7的修改列表的第一个元素中
  • 为索引8和9的前两个元素添加2500(根据对miquelvir的注释)
  • 列表可能是空的
  • 就地修改test_dict

总而言之:

from pprint import pprint

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],
    4: [],
    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],
    8: [],
    9: [2730, 3000, 3800, 4800, 6000, 8500],
}

for k in test_dict:
    try:
        if k <= 7:
            test_dict[k] = test_dict[k][1:]
            test_dict[k][0] += 1000
        else:
            test_dict[k] = test_dict[k][2:]
            test_dict[k][0] += 2500
            test_dict[k][1] += 2500

    except IndexError as e:
        print(f"IndexError at {k}: {e}")

pprint(test_dict)

这段代码非常分散,很容易适应您的需要

干杯

相关问题 更多 >

    热门问题