Numpy附加到python列表的两个元素

时间:2015-02-23 01:16:04

标签: python numpy

data = load('data.npy')

def split_classes(data,col):
    newdata = []
    nclasses = len(unique(data[:,col]))
    classes = [[]] * nclasses
    for row in data:
        classes[int(row[col])].append(copy(row))
    print(len(classes[0]),len(classes[1]),len(data))
    return classes
split_classes(data,-1)

这不是我想做的事情。值被添加到python数组中的每个列表中。这种情况下的输出是: 200 200 200

Example:
Input:
[[4, 2, 0]
 [3, 1, 0]
 [5, 9, 1]]
Output:
[[4, 2, 0],[3, 1, 0]],[5, 9, 1]]

1 个答案:

答案 0 :(得分:1)

在Python中使用语法[[]] * nclasses时,并不意味着您获得nclasses个不同的空列表对象。这意味着您将获得一个长度为nclasses的列表,其中每个元素都是相同空列表的句柄。如果其中一个人经历了追加操作,他们都会这样做。

相反,您可以尝试[[] for i in range(nclasses)]。您可以检查id的不同元素的classes,以验证它们确实具有不同的对象ID。

考虑一个较小的例子:

In [6]: x = [[] for i in range(3)]

In [7]: map(id, x)
Out[7]: [139882701328328, 139882701358288, 139882701358360]

In [8]: x[0].append(1)

In [9]: x
Out[9]: [[1], [], []]

In [10]: y = [[]] * 3

In [11]: y[0].append(1)

In [12]: y
Out[12]: [[1], [1], [1]]

In [13]: map(id, y)
Out[13]: [139882701358216, 139882701358216, 139882701358216]