I have two lists:
survey_points = [ [101,123.456,101.234,99.234] , [102,88.243,211.245,100.834] , [103,13.556,134.234,99.234] , [104,12.345,95.342,99.121] ]
survey_points_attributes = [ [101,305] , [102,306] , [103,305] , [104,308] , [105,310] , [106,307] , [107,306] , [108,305] , [109,305] , [110,307] ]
List 'survey_points' contains point measurements. Format "Point number, distance, angle horizontal, angle vertical" List 'survey_points_attributes' contains point attributes/codes. Format "Point number, point attribute/code"
Using the survey_points list I'd like to add the point attribute/code between point number and distance. The search-key between the two would be the point number. E.g. The first point 101 in survey_list is 101, now 101 shows attribute '305' in survey_points_attributes. This '305' value shall now be inserted just after 101 in survey_list How can I achieve this?
The output/result should look like this:
survey_points = [ [101,305,123.456,101.234,99.234] , [102,306,88.243,211.245,100.834] , [103,305,13.556,134.234,99.234] , [104,308,12.345,95.342,99.121] ]
Thanks in advance for your help.
Try:
mapping = dict(survey_points_attributes)
survey_points = [
[p, mapping[p], *rest] if p in mapping else [p, *rest]
for p, *rest in survey_points
]
print(survey_points)
Prints:
[
[101, 305, 123.456, 101.234, 99.234],
[102, 306, 88.243, 211.245, 100.834],
[103, 305, 13.556, 134.234, 99.234],
[104, 308, 12.345, 95.342, 99.121],
]
OR: if the point ID is not found in attributes, insert None
instead:
survey_points = [[p, mapping.get(p), *rest] for p, *rest in survey_points]