Spaces:
Sleeping
Sleeping
def combine_metadata_with_distance(metadatas, distances): | |
# Flatten the nested lists if they are nested | |
metadatas = metadatas[0] if isinstance(metadatas[0], list) else metadatas | |
distances = distances[0] if isinstance(distances[0], list) else distances | |
print(metadatas) | |
if len(metadatas) != len(distances): | |
raise ValueError("Number of metadata entries must match the number of distances") | |
combined_result = [] | |
for metadata, distance in zip(metadatas, distances): | |
new_metadata = { | |
'title': metadata.get('title', ''), | |
'description': metadata.get('description', ''), | |
'price': metadata.get('price', ''), | |
'totalRatings': metadata.get('totalRatings', 0), | |
'reviewSummary': metadata.get('reviewSummary', ''), | |
'triggerWarning': metadata.get('triggerWarning', ''), | |
'distance': distance | |
} | |
combined_result.append(new_metadata) | |
combined_result.sort(key=lambda x: x['distance']) | |
return combined_result |