Home > Back-end >  How can I update values in this dictionary?
How can I update values in this dictionary?

Time:06-03

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)
                            

CodePudding user response:

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)

CodePudding user response:

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

Converting the inner list to a dict

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])
  • Related