Create multiple lists that meet one condition Python 3.6 -
i have class city:
class city(object): #a city has 2 coordinates in graph(x,y), , set of order def __init__(self, name, x, y, orders): self.name = name self.x = x self.y = y self.orders = orders
i create list of cities:
example:
list_cities = [city1, city2, city3, city4] city1 (0.0, 0.0) [1, 2] city2 (1.0, 0.0) [2, 3] city3 (2.0, 0.0) [1, 3] city4 (3.0, 0.0) [2]
i group cities orders this:
list1 = [city1, city3] list2 = [city1, city2, city4] list3 = [city2, city3]
i've tried dictionaries, list lists , sets still not give me results , not know how else it. leave code have tried:
tours = [] city = city.create(arraycity) in range(0,len(city.orders)): tours[city.orders[i]].append(city)
this outputs:
indexerror: list index out of range
i hope can me. thank much
class city: def __init__(self,name,x,y,orders): self.name = name self.x = x self.y = y self.orders = orders city1 = city('city1',0.0,0.0,[1,2]) city2 = city('city2',1.0,0.0,[2,3]) city3 = city('city3',2.0,0.0,[1,3]) city4 = city('city4',3.0,0.0,[2]) list_cities = [city1, city2, city3, city4] group_cities_by_orders = {} in range(1,len(list_cities)+1): city in list_cities: if in city.orders: if in group_cities_by_orders: group_cities_by_orders[i].append(city.name) else: group_cities_by_orders[i] = [city.name] print(group_cities_by_orders) output: {1: ['city1', 'city3'], 2: ['city1', 'city2', 'city4'], 3: ['city2', 'city3']}
Comments
Post a Comment