简体   繁体   English

python:字典和numpy.array问题

[英]python: dictionary and numpy.array issue

I have a dictionary with arrays (same length) associated to strings. 我有一本字典,其中包含与字符串关联的数组(相同长度)。 My goal is to create a new dictionary with the same keys but cutting the arrays, keeping only the elements I need. 我的目标是用相同的键创建一个新的字典,但剪切数组,仅保留我需要的元素。 I wrote a function to do it but the problem is that it returns a dictionary with the same array (correct cut length) associated to every key, while the print command i put to control show the correct association. 我编写了一个函数来执行此操作,但是问题是它返回的字典具有与每个键关联的相同数组(正确的剪切长度),而我控制的打印命令显示了正确的关联。 Here's the function: 功能如下:

def extract_years(dic,initial_year,final_year):

    dic_extr = {}
    l = numpy.size(dic[dic.keys()[0]])

    if final_year != 2013 : 
        a = numpy.zeros((final_year - initial_year)*251)
    elif final_year == 2013 :
        a = numpy.zeros(l - (initial_year-1998)*251)

    for i in range(0,len(dic)):
        #print i
        for k in range (0,numpy.size(a)):
            a[k] = dic[dic.keys()[i]][(initial_year-1998)*251 + k]          
            #print k

        dic_extr[dic.keys()[i]] = a
        print dic.keys()[i]
        print dic_extr[dic.keys()[i]]


    print dic_extr.keys()
    print dic_extr
    return dic_extr

as I said, print dic_extr[dic.keys()[i]] shows the correct results while the final print dic_extr shows a dictionary with the same array associated to every key. 如我所说, print dic_extr[dic.keys()[i]]显示正确的结果,而最终的print dic_extr显示具有与每个键关联的相同数组的字典。

In Python, every object is a pointer. 在Python中,每个对象都是一个指针。 So, you should have to create a new instance of a for each iteration of the outer for loop. 因此,您必须为外部for循环的每次迭代创建一个新的a实例。 You could do this, for example, initializing the a array inside of that loop, like this: 您可以这样做,例如,在该循环内部初始化a数组,如下所示:

def extract_years(dic,initial_year,final_year):

    dic_extr = {}
    l = numpy.size(dic[dic.keys()[0]])

    for i in range(0,len(dic)):

        if final_year != 2013 : 
            a = numpy.zeros((final_year - initial_year)*251)
        elif final_year == 2013 :
            a = numpy.zeros(l - (initial_year-1998)*251)

        for k in range (0,numpy.size(a)):
            a[k] = dic[dic.keys()[i]][(initial_year-1998)*251 + k]          
            #print k

        dic_extr[dic.keys()[i]] = a
        print dic.keys()[i]
        print dic_extr[dic.keys()[i]]


    print dic_extr.keys()
    print dic_extr
    return dic_extr

Perhaps this is not the most elegant solution, but I think that it should work. 也许这不是最优雅的解决方案,但我认为它应该有效。

I think you ran into the typical problem of mutability and pythons way to define variables: 我认为您遇到了可变性和python定义变量的方法的典型问题:

  1. You define a to be mutable type by using numpy.zeros() . 您可以使用numpy.zeros()a定义为可变类型。
  2. Then you make a have a certain values in it, but you actually have a pointer to a list of pointers, pointing to the actuall values. 然后,您在其中创建a一个特定值,但是实际上您有一个指向实际值的指针列表的指针。
  3. By using dic_extr[dic.keys()[i]] = a you copy this pointer into the dic_extr array, not the list of pointers. 通过使用dic_extr[dic.keys()[i]] = a您可以将此指针复制到dic_extr数组中,而不是指针列表中。
  4. Then you change the objects that the pointer list refers to. 然后,更改指针列表引用的对象。
  5. By using dic_extr[dic.keys()[i]] = a you copy the pointer to the list of pointers into the dic_extr array, again not the pointer list itself. 通过使用dic_extr[dic.keys()[i]] = a可以将指针列表的指针复制到dic_extr数组中,而不是指针列表本身。

In the end both pointer point to the same pointer list. 最后,两个指针都指向相同的指针列表。 Easy example: 简单的例子:

a = [1, 2, 3, 4, 5]
b = a
b[0] = 10
print(a) # returns [10, 2, 3, 4, 5]

You can use dic_extr[dic.keys()[i]] = a[:] to actually make a copy of a. 您可以使用dic_extr[dic.keys()[i]] = a[:]来实际复制a。

Here is also a nice explaination to mutability in python. 这也是对python中可变性的很好解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM