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.
CodePudding user response:
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]
CodePudding user response:
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] ]
for point in survey_points:
for attribute in survey_points_attributes:
if point[0] == attribute[0]:
point.insert(1,attribute[1])
print(survey_points)
CodePudding user response:
The idea is to use a dict
to store the attributes, then use the dict
to update the list
.
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] ]
create a dict from the attribute points
attributes = dict(survey_points_attributes)
update the first item in the point list with the attribute
for point in survey_points: point[0:0] = [attributes.get(point[0], 'NA')]
CodePudding user response:
What do you think?
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] ]
a = []
for i in range(0, len(survey_points[:])):
a.append([survey_points[i][0], survey_points_attributes[i][1], survey_points[i][1], survey_points[i][2], survey_points[i][3]])
survey_points = a
print(f"survey_points = {survey_points[:]}")
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]]