Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python字典发布更新值_Python_Dictionary - Fatal编程技术网

Python字典发布更新值

Python字典发布更新值,python,dictionary,Python,Dictionary,我对python字典有一个问题 import random values = {"A" : [2,3,4], "B" : [2], "C" : [3,4]} # this variable have to store as keys all the values in the lists kept by the variable values, and as values a list of numbers # example of dictionary : # {"2" : [2, 6

我对python字典有一个问题

import random

values = {"A" : [2,3,4], "B" : [2], "C" : [3,4]}


# this variable have to store as keys all the values in the lists kept by the variable values, and as values a list of numbers
# example of dictionary :
# {"2" : [2, 6 ,7 ,8, 8], "3" : [9, 7, 6, 5, 4], "4" : [9, 7, 5, 4, 3]}
dictionary = {}

for x in values.keys():
    listOfKeyX = values[x]
    newValues = []
    for index in range (0, 5):
        newValue = random.randint(0,15)
        newValues.append(newValue)
    for value in listOfKeyX:
        if value in dictionary.keys():
            for index in range (0, 5):
                #i want to update a value in dictionary only if the "if" condition is satisfied
                if(newValues[index] < dictionary[value][index]):
                    dictionary[value][index] = newValues[index]
        else:
            dictionary.setdefault(value, [])
            dictionary[value] = newValues
    print dictionary
随机导入
值={“A”:[2,3,4],“B”:[2],“C”:[3,4]}
#该变量必须将变量值保存的列表中的所有值作为键存储,并将数字列表作为值存储
#字典示例:
# {"2" : [2, 6 ,7 ,8, 8], "3" : [9, 7, 6, 5, 4], "4" : [9, 7, 5, 4, 3]}
字典={}
对于值中的x。键():
listOfKeyX=值[x]
newValues=[]
对于范围(0,5)中的索引:
newValue=random.randint(0,15)
追加(newValue)
对于listOfKeyX中的值:
如果dictionary.keys()中的值为:
对于范围(0,5)中的索引:
#我只想在满足“if”条件时更新字典中的值
如果(newValues[index]
我在试图更改字典值时遇到问题。我只想修改通过key=value选择的pairs键值,但这段代码会更改所有字典值。 你能给我一个解决这个问题的建议吗

我试图解释算法的作用: 它迭代values变量的键,并将链接到键的列表保存在变量listOfKeyX中。 它创建一些由newValues[]保存的随机值。 之后,它在listOfKeyX上迭代 如果从列表中获取的值在dictionary.keys()中不存在,它会将所有newValues列表存储在dictionary[value]中,
如果从列表中获取的值已经存在于dictionary.keys()中,它将获取dictionary[value]保留的列表,并尝试以某种方式对其进行升级。

在第一个循环中,您将运行此代码三次:

dictionary.setdefault(value, [])  # create brand new list
dictionary[value] = newValues  # ignore that and use newValues
这意味着
字典中的每个值都是对同一列表的引用。我仍然不能完全确定您想要的结果是什么,但将上面的行替换为:

dictionary[value] = newValues[:]  # shallow copy creates new list

至少意味着他们不共享引用。

这段代码到底在做什么?
listValues
有哪些
.key
?什么是hashFamily
maxFunction
?hashFamily它只是一个基于hashedIndex返回x的散列值的函数,maxFunction是最大迭代次数,因此它是一个costant值。listValues它是一个函数,返回一组单词或数字,并返回一个dictionary对象。因此,如果它返回一个dictionary,为什么它的名称中有
list
?请读一读你是对的,我很抱歉;下次我会更准确的。然而,我的问题不在于整个代码,而只是当我试图用给定的键更改minWiseMap的值时。minWiseMap这是一个存储{key:[list of hased value]}的字典,当我尝试用给定的键更新这个列表时,代码会更改其他列表值,请阅读链接文章,并提供一个其他用户可以实际运行以重新创建问题的最小示例。耶!我是python的初学者,对它了解不多。不过谢谢!