如何从现有字典填充新字典的键值对?
How can I populate the key value pairs of a new dictionary from an existing dictionary?
我有一本像下面这样的字典。第一个值(括号外)是键。大括号中的第一个值是值。
方括号中的下一个是关于值的元数据。
因此,对于键 185589766
和值 183701781
,字典中有两个条目用于两个不同的书名 - 一个用于 '"Cloud Computing" How to use it for your business'
,另一个用于"Learn How to Be a Success in Network Marketing"
。元数据中的第二个条目是概率值,第三个条目是符号 (+,-)
25255942 {52691892: [('Internet of Things (IOT) Device Management', 0.4444444444444444, 'neg')], 72359602: [('Internet of Things (IOT) Device Management', 1.0, 'neg'), ('Questions', 0.07692307692307693, 'neg')]}
185589766 {183701781: [('"Cloud Computing" How to use it for your business', 0.4, 'pos'), ('Learn How to Be a Success in Network Marketing', 0.16666666666666666, 'pos')], 183702935: [('"Cloud Computing" How to use it for your business', 0.4, 'pos'), ('Learn How to Be a Success in Network Marketing', 0.16666666666666666, 'pos')], 110069642: [('Learn How to Be a Success in Network Marketing', 0.3333333333333333, 'pos'), ('How to make money in network marketing', 1.0, 'pos')]}
110370832 {9420651: [('Dinner and a Movie', 0.5, 'neg'), ("Let's get a Furmeet setup and make a home here", 0.14285714285714285, 'neg'), ('Afterwork Happy Hours and Dinner', 0.5, 'neg')], 76185392: [('Dinner and a Movie', 0.5, 'neg'), ("Let's get a Furmeet setup and make a home here", 0.3333333333333333, 'neg')], 9779381: [("Let's get a Furmeet setup and make a home here", 0.25, 'neg')]}
现在我需要从这个现有词典创建一个新词典。新词典将包含现有词典的键、值、书名,不包括符号和概率值。除此之外,它将在元数据中存储一个 new 条目 'propagation probability'。我该怎么做?
新条目 'propagation probability' 应按以下格式添加到词典中(为键添加示例):
25255942 {52691892: [('Internet of Things (IOT) Device Management', prop_prob1)], 72359602: [('Internet of Things (IOT) Device Management', prop_prob2), ('Questions', prop_prob3)]}
其中 prop_prob1、prop_prob2、prop_prob3 是各自的传播概率。这些传播概率将通过算法计算并存储在新字典中。
试试这个:
new_dict = {}
for key in some_dict:
new_dict[key] = {}
for subkey in some_dict[key]:
prop_prob = # Your calculation here
new_entry = [((some_dict[key][subkey][i])[0], prop_prob)
for i in range(len(some_dict[key][subkey]))]
new_dict[key][subkey] = new_entry
我有一本像下面这样的字典。第一个值(括号外)是键。大括号中的第一个值是值。
方括号中的下一个是关于值的元数据。
因此,对于键 185589766
和值 183701781
,字典中有两个条目用于两个不同的书名 - 一个用于 '"Cloud Computing" How to use it for your business'
,另一个用于"Learn How to Be a Success in Network Marketing"
。元数据中的第二个条目是概率值,第三个条目是符号 (+,-)
25255942 {52691892: [('Internet of Things (IOT) Device Management', 0.4444444444444444, 'neg')], 72359602: [('Internet of Things (IOT) Device Management', 1.0, 'neg'), ('Questions', 0.07692307692307693, 'neg')]}
185589766 {183701781: [('"Cloud Computing" How to use it for your business', 0.4, 'pos'), ('Learn How to Be a Success in Network Marketing', 0.16666666666666666, 'pos')], 183702935: [('"Cloud Computing" How to use it for your business', 0.4, 'pos'), ('Learn How to Be a Success in Network Marketing', 0.16666666666666666, 'pos')], 110069642: [('Learn How to Be a Success in Network Marketing', 0.3333333333333333, 'pos'), ('How to make money in network marketing', 1.0, 'pos')]}
110370832 {9420651: [('Dinner and a Movie', 0.5, 'neg'), ("Let's get a Furmeet setup and make a home here", 0.14285714285714285, 'neg'), ('Afterwork Happy Hours and Dinner', 0.5, 'neg')], 76185392: [('Dinner and a Movie', 0.5, 'neg'), ("Let's get a Furmeet setup and make a home here", 0.3333333333333333, 'neg')], 9779381: [("Let's get a Furmeet setup and make a home here", 0.25, 'neg')]}
现在我需要从这个现有词典创建一个新词典。新词典将包含现有词典的键、值、书名,不包括符号和概率值。除此之外,它将在元数据中存储一个 new 条目 'propagation probability'。我该怎么做?
新条目 'propagation probability' 应按以下格式添加到词典中(为键添加示例):
25255942 {52691892: [('Internet of Things (IOT) Device Management', prop_prob1)], 72359602: [('Internet of Things (IOT) Device Management', prop_prob2), ('Questions', prop_prob3)]}
其中 prop_prob1、prop_prob2、prop_prob3 是各自的传播概率。这些传播概率将通过算法计算并存储在新字典中。
试试这个:
new_dict = {}
for key in some_dict:
new_dict[key] = {}
for subkey in some_dict[key]:
prop_prob = # Your calculation here
new_entry = [((some_dict[key][subkey][i])[0], prop_prob)
for i in range(len(some_dict[key][subkey]))]
new_dict[key][subkey] = new_entry