xx4xx4
xx4xx4

Reputation: 45

storing values from loop

For example I have this code:

for r in res:
    for x,y in r:
        print(x,y)

And the output is this:

[0 0 0 1 0 0 0 0 0 1 1 0 1] (0.3779644730092272, 1)
[1 1 0 0 1 1 0 0 0 0 0 0 0] (0.4472135954999579, 0)
[0 0 1 1 0 0 0 1 0 0 0 0 1] (0.4472135954999579, 1)

But how do I store the first column which is the 0's and 1's in a single variable like this:

[[0 0 0 1 0 0 0 0 0 1 1 0 1]
[1 1 0 0 1 1 0 0 0 0 0 0 0]
[0 0 1 1 0 0 0 1 0 0 0 0 1]]

I also tried this:

for r in res:
    for x,y in r:
        first = x

But this only stores the last value. Is there a neat way or a one liner in python that can do this?

Upvotes: 1

Views: 51

Answers (1)

wim
wim

Reputation: 362557

That looks like a straight list comprehension:

result = [x for r in res for x,y in r]

You seem to be working with 2D arrays of numbers - you might consider using numpy here.

Upvotes: 4

Related Questions