Untitled
unknown
python
a year ago
3.0 kB
4
Indexable
import json import random class LocationSelector: def __init__(self, json_data): self.original_data = json.loads(json_data) self.reset() def reset(self): self.available_coordinates = self.original_data['coordinates'].copy() def select_random_location(self): if not self.available_coordinates: print("All locations have been selected. Resetting the list.") self.reset() selected = random.choice(self.available_coordinates) self.available_coordinates = [coord for coord in self.available_coordinates if coord['id'] != selected['id']] return selected # Example usage json_data = ''' { "coordinates": [ {"id": 1, "lat": 31.7767, "lon": 35.2345, "name": "Jerusalem 1"}, {"id": 2, "lat": 31.7768, "lon": 35.2346, "name": "Jerusalem 2"}, {"id": 3, "lat": 31.7766, "lon": 35.2344, "name": "Jerusalem 3"}, {"id": 4, "lat": 32.0853, "lon": 34.7818, "name": "Tel Aviv"}, {"id": 5, "lat": 31.8969, "lon": 34.8186, "name": "Beit Shemesh"}, {"id": 6, "lat": 32.1840, "lon": 34.8697, "name": "Petah Tikva"}, {"id": 7, "lat": 31.9628, "lon": 34.8061, "name": "Ramla"}, {"id": 8, "lat": 32.0714, "lon": 34.8242, "name": "Givatayim"}, {"id": 9, "lat": 31.9946, "lon": 34.7589, "name": "Rishon LeZion"}, {"id": 10, "lat": 32.0852, "lon": 34.9780, "name": "Rosh HaAyin"}, {"id": 11, "lat": 32.0751, "lon": 34.7752, "name": "Dizengoff Center"}, {"id": 12, "lat": 32.1001, "lon": 34.7745, "name": "Tel Aviv Port"}, {"id": 13, "lat": 32.0724, "lon": 34.7794, "name": "Habima Square"}, {"id": 14, "lat": 32.0684, "lon": 34.7684, "name": "Carmel Market"}, {"id": 15, "lat": 32.0526, "lon": 34.7519, "name": "Jaffa Old City"}, {"id": 16, "lat": 32.0998, "lon": 34.8080, "name": "Yarkon Park"}, {"id": 17, "lat": 32.0778, "lon": 34.7868, "name": "Tel Aviv Museum of Art"}, {"id": 18, "lat": 32.0652, "lon": 34.7765, "name": "Rothschild Boulevard"}, {"id": 19, "lat": 32.0632, "lon": 34.7665, "name": "Neve Tzedek"}, {"id": 20, "lat": 32.0744, "lon": 34.7923, "name": "Azrieli Center"}, {"id": 21, "lat": 32.1118, "lon": 34.8014, "name": "Tel Aviv University"} ] } ''' afeka = {"id": 22, "lat": 32.1134, "lon": 34.8175, "name":"Afeka college"} selector = LocationSelector(json_data) # Select and print 25 random locations (more than the total number to demonstrate reset) for i in range(10): location = selector.select_random_location() print(f"Selection {i+1}: {location['name']} (ID: {location['id']})") print("finished loop 1") for i in range(10): location = selector.select_random_location() print(f"Selection {i+1}: {location['name']} (ID: {location['id']})") print("finished loop 2") for i in range(10): location = selector.select_random_location() print(f"Selection {i+1}: {location['name']} (ID: {location['id']})") print("finished loop 3")
Editor is loading...
Leave a Comment