简体   繁体   English

枚举字典的值

[英]Enumerate values of a dict

I am editing such a dict and am planning to enumrate the values. 我正在编辑这样的字典,并计划枚举值。

梁宁_产品思维30讲 = {
'发刊词':['产品能力是每个人的底层能力', '案例:用户体验与结婚教练'],
'同理心':['同理心训练:怎样理解愉悦与不爽', '同理心训练:怎样理解愤怒与恐惧', '同理心训练:产品要顺应用户潜意识', '认清人的本性,理解角色化生存', '自我与自律, 哪一种更贴近产品精神'],
'机会判断':['机会判断:点线面体的战略选择', '机会判断:怎样找到有势能的趋势', '痛点,痒点,爽点,都是产品机会', '两套经典的用户画像', '设计产品时要包括产品的场景'],
'系统能力':['怎样用系统能力给人提供确定性', '系统效率:小米效率的革命', '系统世界观:微信/米聊/陌陌','系统迭代:微信红包的意外与刻意', '系统生死线:猎豹和它的关键任务'],
'用户体验':["用'用户价值公式'衡量创新", "用'交叉视角'跨界创新", '怎样从新要素到新物种', '三级火箭:深度讲解互联网降维打击', '颠覆式创新:成败价值网'],
'产品世界观':['从产品中的微观, 中观和宏观视角', '人生逻辑大于商业逻辑', '产品连接客观世界/过去与将来']}

Firstly, I keep its order using OrderedDict 首先,我使用OrderedDict保持其顺序

from collections import OrderedDict
d = OrderedDict(梁宁_产品思维30讲)

Secondly, enumerate with dict comprehension 其次,用dict理解枚举

In [46]: {k:list(enumerate(v,start=1)) for item in d.items() for k, v in item}
ValueError: too many values to unpack (expected 2)

Unfortunately, I have to try a complicated method. 不幸的是,我必须尝试一种复杂的方法。

new_d = {}
for item in d.items():
    for k, v in item:
        new_item = {k:list(enumerate(v, start=1))}
    new_d.update(new_item)

it reported the same error again with 它再次报告了相同的错误

ValueError: too many values to unpack (expected 2)

What's the problem with my solutions? 我的解决方案有什么问题?

You can extract keys and values directly during the iteration over your dictionary: 您可以在字典的迭代过程中直接提取键和值:

new_d = {}
for k, v in d.items():
    new_item = {k:list(enumerate(v, start=1))}
    new_d.update(new_item)

print(new_d)

This translates into your dictionary comprehension as 这将转化为您的字典理解为

{k:list(enumerate(v,start=1)) for k, v in d.items()}

In your code item is a tuple, representing a key, value pair. 在您的代码item是一个元组,表示一个键,值对。 You now tell Python to iterate over the tuple with your line 现在,您告诉Python用您的行遍历元组

 for k, v in item:

and unpack values from each element for exactly two variables. 并从每个元素中解包出恰好两个变量的值。 That fails, because your key is longer than just two characters. 失败了,因为您的密钥超过了两个字符。 You can see a simulation of this behaviour in the following example (I use here a list of tuples, because the order in a dictionary is not defined): 在以下示例中,您可以看到这种行为的模拟(我在这里使用元组列表,因为未定义字典中的顺序):

dict_item = [("AB", [3, 4], "a7"), ("xy", "12"), ("de", [5, 6, 7], 43)]

for item in dict_item:
    for k, v in item:
        print(k, v)

As you can see, everything works fine, as long as the elements are iterable (string or list) and have exactly two elements to unpack. 如您所见,只要元素是可迭代的(字符串或列表)并且有正好要解包的两个元素,那么一切都可以正常工作。 If the element is not iterable (like the integer 43 ), Python tells you that the object is not iterable. 如果该元素不可迭代(如整数43 ),Python会告诉您该对象不可迭代。 If the element is longer, it tells you that there are more values than expected from the variables - your "too many values to unpack (expected 2)" error message. 如果元素更长,则它告诉您变量中的值比预期的要多-您的“解压缩的值太多(预期2)”错误消息。 The opposite can happen, too, that there are less values than variables. 相反的情况是,值比变量少。 Then you would get the error message "not enough values to unpack (expected 2, got 1)". 然后,您将收到错误消息“没有足够的值要解压(预期2,得到1)”。

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

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