Tung Nguyen
Tung Nguyen

Reputation: 390

How to mapping list of points with list coordinates by keyname?

Please help me, I have 2 list: List of coordinates:

coordinateArr= [[A,190,340],[B,270,580],[C,395,140]]

List of Rountines:

path=[A,B,C,A,B]

How to map list path with coordinateArr, the result like this:

result=[[190,340],[270,580],[395,140],[190,340],[270,580]]

Many thanks for your help!

Upvotes: 0

Views: 53

Answers (2)

nauer
nauer

Reputation: 700

coordinateArr= [['A',190,340],['B',270,580],['C',395,140]]
path=['A','B','C','A','B']

[a[1:] for p in path for a in coordinateArr if p==a[0]]   

Output:

[[190, 340], [270, 580], [395, 140], [190, 340], [270, 580]]

This is more or less the same like:

output = []

for p in path:
   for a in coordinateArr:    
      if p == a[0]:
         output.append(a[1:])

Upvotes: 1

Chris
Chris

Reputation: 29742

Make coordinateArr as dict:

coordinateArr= [['A',190,340],['B',270,580],['C',395,140]]
path=['A','B','C','A','B']

d = {i[0]:i[1:] for i in coordinateArr}

[d.get(p) for p in path]

Output:

[[190, 340], [270, 580], [395, 140], [190, 340], [270, 580]]

Upvotes: 1

Related Questions