How to create an array by using the index of other arrays in Python 2.7?

I have three arrays IDs Edges Xn, I need to create a fourth array using this information. IDs contain different IDs:

IDs = ['48', '63', '77', '108', '306']

In Edges are a pair of IDs:

Edges = [('48', '77'), ('63', '48'), ('77', '48'), ('77', '108'), ('108', '48'), ('306', '48')])

and Xn the coordinates of the respective ID:

Xn = [3.08, 4.96, 4.04, 1.68, 1.6]

I would like to create an array Xe combining the information of the three arrays, i.e., where the positions of the IDs are identified in the array Edges, e.g., for edge ('48', '77') are positions 1 and 3 in IDs (or 0 and 2 in Python), so use the first and third coordinates in Xn (3.08, 4.04) and do the same for all the edges separated by the word 'None' so I can have an array Xe like this:

Xe = [3.08, 4.04, None, 4.96, 3.08, None, 4.04, 3.08, None, 4.04, 1.68, None, 1.68, 3.08, None, 1.60, 3.08, None]

If you can help me I would really appreciate it!

Cheers!

Upvotes: 1

Views: 47

Answers (2)

Developer
Developer

Reputation: 178

Tested work perfect

IDs = ['48', '63', '77', '108', '306']
Edges = [('48', '77'), ('63', '48'), ('77', '48'), ('77', '108'), ('108', '48'), ('306', '48')]
Xn = [3.08, 4.96, 4.04, 1.68, 1.6]
Xe = []

for i in xrange(len(Edges)):
    for j in xrange(2):
        if Edges[i][j] in IDs:
            Xe.append(Xn[IDs.index(Edges[i][j])])
    Xe.append(None)

print Xe

Upvotes: 1

Alberto
Alberto

Reputation: 1489

IDs = ['48', '63', '77', '108', '306']
Edges = [('48', '77'), ('63', '48'), ('77', '48'), ('77', '108'), ('108', '48'), ('306', '48')])
Xn = [3.08, 4.96, 4.04, 1.68, 1.6]
Xe = [0]*len(Edges)*3
def findid(arrayl,num):
 For i in range (arrayl):
  if arrayl[i]==num:
   return i
 return -1
j=0
For i in range (len(Edges)):
 j+=1
 Xe[j]=Xn[findid(IDs,Edge[i][0])]
 j+=1
 Xe[j]=Xn[findid(IDs,Edge[i][1])]
 Xe[j+1]="None"
 j+=2

I think this may work, i did without python interpreter, so I didnt tested it, try it and say if it workds! :)

Upvotes: 0

Related Questions