Issue
I have a dictionary of Line ID's and their 2 corresponding Point X,Y coordinates. The 2 points are not guaranteed to be in the order of start, end points. And I need to sort the ID's in order of how they are connected, into a list.
d= { 34:((3,5), (2,5)), 82:((1,1), (1,2)), 2:((8,4), (3,5)), 13:((1,2), (2,5))}
I'll have a startEdge and startPt example: startEdge = 82 startPt = 1,1
I'm thinking I need some kind of iterator, but have not had much success. My project could have up to 200+ line segments, so I'm trying not to slow the script down. Any help would be great
edit: here is what I started to try so far... but not sure how to get this in a loop
def get_key(val, my_dict):
for key, value in my_dict.items():
if val in value[0]:
currEdge = key
currPt = value[0]
nextPt = value[1]
if val in value[1]:
currEdge = key
currPt = value[1]
nextPt = value[0]
Some Clarification, Sometimes the startEdge will have a few other edges on the side I don't want the path to go, therefore I need to give it a direction, or a "startPoint"
Solution
Here is one solution. I would be interested to see how it performs on a larger dictionary.
def create_path(start_edge, start_point, dic):
lst=[]
edge = start_edge
tail = start_point
while len(dic.keys()) > len(lst):
head = next(filter(lambda x: x != tail, dic[edge]))
for e, points in dic.items():
if head in points and e not in lst:
lst.append(e)
edge = e
tail = head
return lst
d= { 34:((3,5), (2,5)), 82:((1,1), (1,2)), 2:((8,4), (3,5)), 13:((1,2), (2,5))}
print(create_path(82, (1,1),d))
#output: [82, 13, 34, 2]
For Python 2.7, simply replace the lambda expression with this block of code to find the head:
if tail == dic[edge][0]:
head = dic[edge][1]
else:
head = dic[edge][0]
Answered By - pakpe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.