简体   繁体   English

如何使用枚举为字典理解添加额外的值?

[英]How do you add an extra value to a dictionary comprehension with enumerate?

I want to be able to access my_cal[30]['r'] and get all the ranges.我希望能够访问my_cal[30]['r']并获取所有范围。 Then access the first week with my_cal[30]['wk']然后使用my_cal[30]['wk']访问第一周

I tried this:我试过这个:

new_ranges = {k:{'r':value,'week':i}, for i,(k,v) in enumerate(my_cal.items(),1)}

This is the example code:这是示例代码:

my_cal = { 'somekey':1,
            'ranges':{ 30:['08-1-2021'],
                      31: [   '08-2-2021',
                              '08-3-2021',
                              '08-4-2021',
                              '08-5-2021',
                              '08-6-2021',
                              '08-7-2021',
                              '08-8-2021'],
                      32: [   '08-9-2021',
                              '08-10-2021',
                              '08-11-2021',
                              '08-12-2021',
                              '08-13-2021',
                              '08-14-2021',
                              '08-15-2021']},
                      'lastkey':1}

Wanted result:想要的结果:

my_cal = { 'somekey':1,
    'ranges':{ 30:'r':['08-1-2021'],'wk':1},
                31: {'r':[   '08-2-2021',
                              '08-3-2021',
                              '08-4-2021',
                              '08-5-2021',
                              '08-6-2021',
                              '08-7-2021',
                              '08-8-2021']},{'week',2},
                      'lastkey':1},

This print does not work in this example bellow because it complains about the tuple object has no attribute items.此打印在下面的示例中不起作用,因为它抱怨元组 object 没有属性项。 It works fine in my real code.它在我的真实代码中运行良好。 I don't know why it converts it to a tuple.我不知道为什么它将它转换为元组。

for key,value in my_cal.items():
    print("key is:", key, " value is:" , "\n")

You have some typos in your code, and one error:您的代码中有一些拼写错误,还有一个错误:

  1. In the definition of my_cal , you end it with a comma so that makes it the first element of a tuple.my_cal的定义中,以逗号结尾,使其成为元组的第一个元素。 Remove that last trailing comma.删除最后一个尾随逗号。

  2. For new_ranges , you're doing for (k, v) in... but then you refer to it as value .对于new_ranges ,您正在for (k, v) in...执行操作,但随后您将其称为value

  3. An extra comma , after the value part of the dict comprehension.字典理解的部分,的额外逗号。

Also, you should be iterating over my_cal['ranges'].items() , as Barmar mentioned .此外,您应该迭代my_cal['ranges'].items()正如 Barmar 提到的那样 Anyway, if you change that line to:无论如何,如果您将该行更改为:

new_ranges = {k: {'r': v, 'week': i} for i, (k, v) in enumerate(my_cal['ranges'].items(), 1)}

It gives you the new_ranges you expect.它为您提供了您期望的new_ranges You can then assign it back to ranges in my_cal to get the form you want:然后,您可以将其分配回my_cal中的ranges以获得您想要的形式:

my_cal['ranges'] = new_ranges

# output:

{'somekey': 1,
 'ranges': {30: {'r': ['08-1-2021'], 'week': 1},
            31: {'r': ['08-2-2021',
                       '08-3-2021',
                       '08-4-2021',
                       '08-5-2021',
                       '08-6-2021',
                       '08-7-2021',
                       '08-8-2021'],
                 'week': 2},
            32: {'r': ['08-9-2021',
                       '08-10-2021',
                       '08-11-2021',
                       '08-12-2021',
                       '08-13-2021',
                       '08-14-2021',
                       '08-15-2021'],
                 'week': 3}},
 'lastkey': 1}

Edit: Wrt "I want to be able to access my_cal[30]['r'] and get all the ranges. Then access the first week with my_cal[30]['wk'] " - then make the following changes after the new_ranges = {...} step:编辑: Wrt “我希望能够访问my_cal[30]['r']并获取所有范围。然后使用my_cal[30]['wk']访问第一周” - 然后new_ranges = {...}步骤:

del my_cal['ranges']  # deletes the original `ranges` key
my_cal.update(new_ranges)  # adds the new ranges

# result:
{'somekey': 1,
 'lastkey': 1,
 30: {'r': ['08-1-2021'], 'week': 1},
 31: {'r': ['08-2-2021',
            '08-3-2021',
            '08-4-2021',
            '08-5-2021',
            '08-6-2021',
            '08-7-2021',
            '08-8-2021'],
      'week': 2},
 32: {'r': ['08-9-2021',
            '08-10-2021',
            '08-11-2021',
            '08-12-2021',
            '08-13-2021',
            '08-14-2021',
            '08-15-2021'],
      'week': 3}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM