我如何更新这本字典中的值?

How can I update values in this dictionary?

我有一本像下面这样的字典。在方括号中有关于值的元数据。元数据中的第一个值是连接名称,元数据中的第二个值是概率。

所以对于键25255942,对于值52691892,对于连接Internet of Things (IOT) Device Management,概率值为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),
        ],
    },
}

现在我需要更新键、值和连接的特定组合的概率值。为此,我编写了以下函数,它不会修改所需的概率值。我该怎么做?

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)
                            

编辑

这里我提供了部分字典,因为它不适合这里:

 {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)]}}

现在请检查是否可以使用您的代码将三元组 <25255942,72359602,'Questions'> 的相应值从 0 更改为 1。

查看您的数据结构,最里面的元素是 tuplelist

这是一个问题。假设你有一个这样的内部列表:

[('a', 12), ('b', 3), ('f', 7), ('a', 14), ('j', 5)]

如果要查找或修改'a',你会怎么做?

所以我建议将元组列表也转换为字典:

{
    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,
        },
    },
}

这样你就可以分配一个新值:

dictionary[key][value][connection] = prob

将内部列表转换为字典

如果你有一个 2 元组列表,你可以将它转换为一个列表(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}

所以像这样的函数应该做到:

def modify(dictionary):
    for j in dictionary:
        for k in j:
            dictionary[j][k] = dict(dictionary[j][k])

在不改变数据结构的情况下,可以通过dict.get和一次迭代修改数据:

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

如果您想将数据结构更改为@Roland Smith 所说的,您可以:

for v1 in dictionary.values():
    for k, v2 in v1.items():
        v1[k] = dict(v2)