1
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)

This just isn't doing what I want it to do. Values are being added to each list within the python array. The output in this case being: 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
  • Can you put some example input and output. It's hard to understand what you want to do and what's going wrong without seeing your data before and after... Commented Feb 23, 2015 at 1:20

1 Answer 1

1

When you use the syntax [[]] * nclasses in Python, it does not mean you get nclasses number of distinct empty list objects. It means you get a list of length nclasses where each element is a handle to the same empty list. If one of them experiences an append operation, they all do.

Instead you could try [[] for i in range(nclasses)]. You can inspect the id of the different elements of classes to verify that indeed they have distinct object ids.

Consider a smaller example:

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]
Sign up to request clarification or add additional context in comments.

1 Comment

In general, this means that it's OK to use the whole [x] * k trick to get k copies of x whenever x is something immutable, like a string, integer, float, or tuple. But when x is mutable, don't use the [x] * k trick, just explicitly initialize each location in the length-k array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.