Python : creating an array from list with given indices -


i know ought simple, still stuck :-/

i have 2 equal-length arrays , b. there 3 equal-length lists al1, al2, al3 : have number pairs such (a[i],b[i])=(al1[j],al2[j]). have put these indices of , j in aindex , lindex respectively. want create array c (equal length of array a) containing numbers al3.

see code:

import numpy np  list1 = [9,7,8,2,3,4,1,6,0,7,8] = np.asarray(al) list2 = [5,4,2,3,4,3,4,5,5,2,3] b = np.asarray(bl)  al1 = [9,4,5,1,7] al2 = [5,3,6,4,5] al3 = [5,6,3,4,7]  lindex = [0,5,6] aindex = [0,1,3] index = np.asarray(aindex)  c = [] in range(0,len(list1)):     if in aindex:     com = al3[i] else:     com = 'nan'  c.append(com) 

what

c = [5, 6, 'nan', 4, 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan'] 

what want is

c = [5, 'nan', 'nan', 'nan', 'nan', 6, 4, 'nan', 'nan', 'nan', 'nan'] 

please :-/

i must admit have no clue you're doing, seem want this:

>>> c = ['nan'] * len(a) >>> i, j in zip(aindex, lindex): ...     c[j] = al3[i] ...  >>> c [5, 'nan', 'nan', 'nan', 'nan', 6, 4, 'nan', 'nan', 'nan', 'nan'] 

they way works first initializing c list of length a containing nans:

>>> c = ['nan'] * len(a) >>> c ['nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan', 'nan'] 

what zip pair elements of each list:

>>> zip(aindex, lindex) [(0, 0), (1, 5), (3, 6)] 

so iterate through each pair, taking value of al3 @ first index , assigning c @ second index. unassigned remains 'nan'.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -