As @Tyler Chen observed, you can't slice a Python list of tuples with Numpy-like slicing (or at least I don't know a way). There are several ways to approach the problem:
- loop over the list with an index
- use
map
and itemgetter
- create a dictionary of the elements:
dict()
- use
zip
in the list elements
Details about each in this SO answer:
How to extract the n-th elements from a list of tuples in python?
None of the examples show how this works with an array as a List/Tuple element. So, I created a simple example using the loop method (easiest for a new user to comprehend). The code above the +-+-+-+
comment creates data similar to yours (if I understand the problem statement). The code below the comment shows how I would do it. I looped on the index because I use it as the row number when adding to the array. You can also loop on the list elements, and add a row counter for that purpose (see alternate method).
This example has 10 rows and 20 values in each eigenvector. I will let you generalize for N rows and M vectors.
Note: Numpy array size is allocated in advance. So, in general you will need to scan your tuples to determine the number of rows and columns to allocate the array before the loop.
import numpy as np
# First, build some simple Eigenvalue and Eigenvector data
# 10 Eigenvalues in eig_vals and Eigenvectors as 20 values in 10 rows (shape [10,20])
eig_vals = np.logspace(0.1,3., num=10)
eig_vec = np.zeros([10,20])
for i in range(len(eig_vals)) :
eig_vec_r = np.random.random_sample(20)
eig_vec[i,:]=eig_vec_r
# Create List of tuples for each eig_vals, eig_vec pair:
eigen_pairs = [(eig_vals[i], eig_vec[i,:]) for i in range(len(eig_vals)) ]
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# create new nd.array to hold extrated Eigenvector data (shape [10,20])
saved_eig_vec = np.zeros([10,20])
# loop thru List of tuples to extract Eigenvector array and write to saved_eig_vec
# Each Eigenvector will be in 1 row
for i in range(len(eigen_pairs)) :
saved_eig_vec[i,:] = eigen_pairs[i][1]
#Verify correctness by comparing the following values:
# 1) eig_vec[9,:] initial array created with random_sample function
# 2) eigen_pairs[9][1] - array saved in list of tuples
# 3) saved eig_vec[9,:] array extrated and saved from list of tuples
print (eig_vec[9,:])
print (eigen_pairs[9][1])
print (saved_eig_vec[9,:])
# Alternate method, looping on list elements with row counter (reset saved_eig_vec array)
saved_eig_vec = np.zeros([10,20])
row = 0
for i in eigen_pairs :
saved_eig_vec[row,:] = i[1]
row += 1
print (saved_eig_vec[9,:])