Python 如何在字典列表上循环时更改全局字典?

Python 如何在字典列表上循环时更改全局字典?,python,dictionary,global,Python,Dictionary,Global,在update_data()函数中,我声明字典是全局的,但一旦我循环并将它们分配给局部变量,我就不能再更改全局dict 我做的骇人听闻的黑客程序说明了它应该如何工作。但是,如果我必须向列表中添加许多新的字典对,它将变得不可行,因为我还必须添加到if..elif语句中。它很快变得很难看 是否有一种“恰当的”、“优雅的”或“pythonic”的方式来为全球词典赋值? 显然,我使用的是python2.7,但是如果python3.x有不同的解决方案,请随意评论,因为它很快就会被迁移 # global d

update_data()
函数中,我声明字典是全局的,但一旦我循环并将它们分配给局部变量,我就不能再更改全局
dict

我做的骇人听闻的黑客程序说明了它应该如何工作。但是,如果我必须向列表中添加许多新的字典对,它将变得不可行,因为我还必须添加到
if..elif
语句中。它很快变得很难看

是否有一种“恰当的”、“优雅的”或“pythonic”的方式来为全球词典赋值? 显然,我使用的是
python2.7
,但是如果
python3.x
有不同的解决方案,请随意评论,因为它很快就会被迁移

# global dicts
temps = {0: 25, 1: 28, 2: 29, 3: 28}  # ...
temps_old = {}
flows = {0: 1.23, 1: 2.45}  # ...
flows_old = {}
# ...


def update_data():
    global temps, temps_old, flows, flows_old
    # loop and create list of changes
    for my_list, my_list_old, code in [(temps, temps_old, 't'), (flows, flows_old, 'f')]:
        xmlsnip = ''
        for key, value in my_list.iteritems():
            include = True
            if key in my_list_old:
                if my_list_old[key] == value:
                    include = False  # block it the data has not changed
            if include:
                my_list[key] = False
                tag = "{0}{1:02d}".format(code,int(key))
                xmlsnip += '<{0}>{1}</{2}>'.format(tag, value, tag)
        if xmlsnip != '':
            #  What I'd lik eto do is assign to the global list:
            my_list_old = my_list.copy()
            # but it only assigns to the local variable, not the global one I 'pointed' to
            if code == 't':
                temps_old = my_list.copy()
            elif code == 'f':
                flows_old = my_list.copy()
            print '     ' + xmlsnip
        else:
            print '     no changes for ' + code

print '## test update_data finds all differences ##'
update_data()
print '## change 2 temps and 1 flow ##'
temps[2] = 30
temps[1] = 29
flows[0] = 1.25
print '## test that update only finds the 3 changes ##'
update_data()
print '## make no changes and verify it finds nothing ##'
update_data()
#全局命令
temps={0:25,1:28,2:29,3:28}。。。
temps_old={}
流量={0:1.23,1:2.45}#。。。
流_old={}
# ...
def更新_数据():
全球临时,临时旧,流动,流动旧
#循环并创建更改列表
对于my_list,my_list_old,在[(temps,temps_old,'t'),(flows,flows_old,'f')中编码:
xmlsnip=''
对于键,我的_列表中的值。iteritems():
include=True
如果输入我的\u列表\u old:
如果my_list_old[key]==值:
include=False#如果数据未更改,则将其阻止
如果包括:
我的列表[键]=错误
tag=“{0}{1:02d}”。格式(代码,int(键))
xmlsnip+='{1}'。格式(标记、值、标记)
如果xmlsnip!='':
#我想做的是分配到全局列表:
my_list\u old=my_list.copy()
#但它只分配给局部变量,而不是我“指向”的全局变量
如果代码==“t”:
temps\u old=my\u list.copy()
elif代码==“f”:
flows\u old=my\u list.copy()
打印“”+xmlsnip
其他:
打印“不更改”+代码
打印“###测试更新#数据查找所有差异##”
更新_数据()
打印“###更改2个临时和1个流程##”
临时工[2]=30
临时工[1]=29
流量[0]=1.25
打印“###测试更新仅查找3个更改”
更新_数据()
打印“###不做任何更改,并验证未找到任何内容”
更新_数据()
输出如下所示:

## test update_data finds all differences ##
     <t00>25</t00><t01>28</t01><t02>29</t02><t03>28</t03>
     <f00>1.23</f00><f01>2.45</f01>
## change 2 temps and 1 flow ##
## test that update only finds the 3 changes ##
     <t01>29</t01><t02>30</t02>
     <f00>1.25</f00>
## make no changes and verify it finds nothing ##
     no changes for t
     no changes for f
##测试更新#数据查找所有差异##
25282928
1.232.45
##改变2个温度和1个流量##
##测试更新仅查找3个更改##
2930
1.25
##不做任何更改,并验证它找不到任何内容##
t没有变化
f没有变化

问题是您需要在不创建新对象的情况下重置全局字典的元素

请尝试此更新:

    if xmlsnip != '':
        #  What I'd lik eto do is assign to the global list:
        my_list_old.clear()  # remove existing entries
        my_list_old.update(my_list)  # add new entries, keep object pointer
        # but it only assigns to the local variable, not the global one I 'pointed' to