Reputation: 969
I have a dictionary like below. In the square brackets there are metadata about the values. The first value in the metadata is the connection name, the second value in the metadata is a probability.
So for the key 25255942
, for the value 52691892
, and for the connection Internet of Things (IOT) Device Management
, the probability value is prob1
.
{
25255942: {
52691892: [("Internet of Things (IOT) Device Management", prob1)],
72359602: [
("Internet of Things (IOT) Device Management", prob2),
("Questions", prob3),
],
},
185589766: {
183701781: [
('"Cloud Computing" How to use it for your business', prob4),
("Learn How to Be a Success in Network Marketing", prob5),
],
183702935: [
('"Cloud Computing" How to use it for your business', prob6),
("Learn How to Be a Success in Network Marketing", prob7),
],
110069642: [
("Learn How to Be a Success in Network Marketing", prob8),
("How to make money in network marketing", prob9),
],
},
}
Now I need to update a probability value for a particular combination of key, value and connection. For that I wrote the following function which doesn't modify the desired probability value. How can I do this?
def modify_sign_a(v, x, connection, dictionary, value):
for node in dictionary:
if node == v:
for follower in dictionary[node]:
if follower == x
for follower_info in dictionary[node][follower]:
if follower_info[0] == connection:
follower_info = list(follower_info)
follower_info[1] = follower_info[1] + value
follower_info = tuple(follower_info)
EDIT
Here I'm providing partial dictionary as it doesn't fit here fully:
{25255942: {52691892: [('Internet of Things (IOT) Device Management', 0)], 72359602: [('Internet of Things (IOT) Device Management', 0), ('Questions', 0)]}, 185589766: {183701781: [('"Cloud Computing" How to use it for your business', 0), ('Learn How to Be a Success in Network Marketing', 0)], 183702935: [('"Cloud Computing" How to use it for your business', 0), ('Learn How to Be a Success in Network Marketing', 0)], 110069642: [('Learn How to Be a Success in Network Marketing', 0), ('How to make money in network marketing', 0)]}, 110370832: {9420651: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Afterwork Happy Hours and Dinner', 0)], 76185392: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0)], 9779381: [("Let's get a Furmeet setup and make a home here", 0)]}, 86319132: {101605182: [('Line Dancing to All Kinds of Music', 0), ('Why We Need Highly Sensitive People', 0), ("Meeting other SAHM's", 0), ('Seminars on First Time Home Buyers', 0)], 97636712: [('Public Speaking as a Means to Market your Business', 0), ('Where or where to go on a honeymoon?', 0), ('Reading and discussion', 0)]}, 40681502: {9024523: [('Activities', 0), ('Dinner and a Movie', 0), ('"Cloud Computing" How to use it for your business', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0)], 40323812: [('Activities', 0), ('"Cloud Computing" How to use it for your business', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0), ('Dining Out, BBQs, Food Fairs, Happy Hour and More', 0)], 12747253: [('Activities', 0), ("Let's get a Furmeet setup and make a home here", 0)], 110370832: [('Dinner and a Movie', 0), ('How Do I Begin My Home Search', 0)], 9420651: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('Meeting other parents', 0)], 123979682: [('Dinner and a Movie', 0), ('Healthy Cooking For You and Your Family', 0), ('"Cloud Computing" How to use it for your business', 0)], 76185392: [('Dinner and a Movie', 0), ("Let's get a Furmeet setup and make a home here", 0), ('"Cloud Computing" How to use it for your business', 0)], 13949877: [("Let's get a Furmeet setup and make a home here", 0), ('How Do I Begin My Home Search', 0), ('Learn How to Stop Foreclosure', 0)]}, 47390772: {96547882: [('Business start up', 0), ('How to start a business', 0), ('start a business', 0)], 60915162: [('Business start up', 0)], 12480975: [('Business start up', 0)], 80749802: [('Business start up', 0)]}, 12017724: {3805969: [('Learn How to Be a Success in Network Marketing', 0), ('What to look for when purchasing a new home', 0)]}}
Now please check if for the triplet <25255942,72359602,'Questions'>
the corresponding value can be changed to 1 from 0 with your code.
Upvotes: 0
Views: 75
Reputation: 43495
Looking at your data structure, the innermost element is a list
of tuple
.
That presents a problem. Suppose you have an inner list like this:
[('a', 12), ('b', 3), ('f', 7), ('a', 14), ('j', 5)]
What will you do if you have to look up or modify 'a'
?
So I would suggest converting that list ot tuples to a dictionary as well:
{
25255942: {
52691892: {"Internet of Things (IOT) Device Management": prob1},
72359602: {
"Internet of Things (IOT) Device Management": prob2,
"Questions": prob3,
},
},
185589766: {
183701781: {
'"Cloud Computing" How to use it for your business': prob4,
"Learn How to Be a Success in Network Marketing": prob5,
},
183702935: {
'"Cloud Computing" How to use it for your business': prob6,
"Learn How to Be a Success in Network Marketing": prob7,
},
110069642: {
"Learn How to Be a Success in Network Marketing": prob8,
"How to make money in network marketing": prob9,
},
},
}
That way you can just assign a new value:
dictionary[key][value][connection] = prob
If you have a list of 2-tuples, you can just cast it to a list (example in IPython):
In [1]: L = [('a', 12), ('b', 3), ('f', 7), ('j', 5)]
Out[1]: [('a', 12), ('b', 3), ('f', 7), ('j', 5)]
In [2]: dict(L)
Out[2]: {'a': 12, 'b': 3, 'f': 7, 'j': 5}
So a function like this should do it:
def modify(dictionary):
for j in dictionary:
for k in j:
dictionary[j][k] = dict(dictionary[j][k])
Upvotes: 1
Reputation: 7736
Without changing the data structure, you can modify the data through dict.get
and one iteration:
def modify_sign_a(v, x, connection, dictionary, value):
lst = dictionary.get(v, {}).get(x)
if lst is None:
return
for i, (connect, prob) in enumerate(lst):
if connect == connection:
lst[i] = connect, prob + value
If you want to change the data structure to what @Roland Smith says, you can:
for v1 in dictionary.values():
for k, v2 in v1.items():
v1[k] = dict(v2)
Upvotes: 1