cerebrou
cerebrou

Reputation: 5540

From a list of vectors, extract only one column from each vector

Given a list of vectors:

[((0, 2.6147858445098677), (1, 1.0257184186249431)), ((0, 2.6147858445098677), (2, 0.34113605903013322)), ((0, 2.6147858445098677), (3, 0.074196986667672063)), ((1, 1.0257184186249431), (2, 0.34113605903013322)), ((1, 1.0257184186249431), (3, 0.074196986667672063)), ((2, 0.34113605903013322), (3, 0.074196986667672063))]

how to extract only the first entry in each vector?

[(0, 1), (0,2), (0, 3), (1, 2), (1,3), (2, 3)]

Upvotes: 1

Views: 54

Answers (4)

blhsing
blhsing

Reputation: 106455

For completeness sake you can also use a combination of operator.itemgetter, map and functools.partial (given a list v):

from operator import itemgetter
from functools import partial
list(map(tuple, map(partial(map, itemgetter(0)), v)))

Or it can be more concise with Python 2.7 or before:

map(partial(map, itemgetter(0)), v)

Upvotes: 0

vash_the_stampede
vash_the_stampede

Reputation: 4606

lista =[
    ((0, 2.6147858445098677), (1, 1.0257184186249431)), 
    ((0, 2.6147858445098677), (2, 0.34113605903013322)),
    ((0, 2.6147858445098677), (3, 0.074196986667672063)), 
    ((1, 1.0257184186249431), (2, 0.34113605903013322)), 
    ((1, 1.0257184186249431), (3, 0.074196986667672063)), 
    ((2, 0.34113605903013322), (3, 0.074196986667672063)) ]

new_lista = []

for i in lista:
    new_lista.append((i[0][0], i[1][0]))

print(new_lista)

Or:

[new_lista.append((i[0][0], i[1][0])) for i in lista]
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 vector.py
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71570

Or map:

>>> l=[((0, 2.6147858445098677), (1, 1.0257184186249431)), ((0, 2.6147858445098677), (2, 0.34113605903013322)), ((0, 2.6147858445098677), (3, 0.074196986667672063)), ((1, 1.0257184186249431), (2, 0.34113605903013322)), ((1, 1.0257184186249431), (3, 0.074196986667672063)), ((2, 0.34113605903013322), (3, 0.074196986667672063))]
>>> list(map(lambda x: (x[0][0],x[1][0]),l))
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
>>> 

Upvotes: 0

Matt Messersmith
Matt Messersmith

Reputation: 13747

Could just use a list comprehension. Each item is a tuple that contains two tuples, so we can parse out the first item of each inner tuple like so:

x = [((0, 2.6147858445098677), (1, 1.0257184186249431)), ((0, 2.6147858445098677), (2, 0.34113605903013322)), ((0, 2.6147858445098677), (3, 0.074196986667672063)), ((1, 1.0257184186249431), (2, 0.34113605903013322)), ((1, 1.0257184186249431), (3, 0.074196986667672063)), ((2, 0.34113605903013322), (3, 0.074196986667672063))]
result = [(item[0][0], item[1][0]) for item in x]
print(result)

outputs:

[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

as desired. This is very c-style, you can get a more pythonic if you want and use tuple unpacking:

result = [(first[0], second[0]) for  first, second in x]

which is a bit more understandable/readbale.

Upvotes: 4

Related Questions