-2
I’m trying to insert an edge into a graph. The problem is to represent a metro line, considering the existing information in csv files. I created the classes Vertex
, Edge
, Station(Vertex)
, Edge_Line(Edge)
and the GraphList
.
Com este método, consigo inserir os vértices do grafo:
def create_stations(filename='lisbon.stations.csv'):
stations = []
with open(filename, 'r', newline='') as stations_info:
station_info = csv.reader(stations_info, delimiter=',')
# skip first line, which is just attribute names
next(station_info)
# empty list to hold stations
# parse all the lines and create stations
for row in station_info:
#print(row[3],row[2])
id = row[0]
latitude = row[1]
longitude = row[2]
nome = row[3]
# assign each attribute of Station based on line in file
#(id, latitude, longitude, nome) = tuple(row)
#create Station instance with those attributes
newstation = Station(id, latitude, longitude, nome)
#print(newstation)
stations.append(newstation)
return stations
map = GraphList()
def station_network():
for station in create_stations():
map.insert_vertex(station)
return map
I created this method to try to get links:
def create_connections(filename = 'lisbon.connections.csv'):
connections = []
with open(filename, 'r', newline='') as conections_info:
conection_info = csv.reader(conections_info, delimiter=',')
next(conection_info)
for row in conection_info:
(origem, destino, linha) = tuple(row)
connections.append(Edge_Line(origem, destino, linha))
return connections
However, when I try to create the edges, I get the error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:/Users/sandr/PycharmProjects/teste6/main.py", line 282, in station_network
map.insert_egde(connection._origem, connection.destination)
File "C:/Users/sandr/PycharmProjects/teste6/main.py", line 217, in insert_egde
self._outgoing[u].append(edge)
KeyError: '1'
How can I solve this problem?
Hi, the part where is happening the line where the error occurs does not seem to be in the code you listed, precisely the
insert_egde(()
but I think the problem here is that you are looking for the '1' key in the dictionary, which does not exist. If so, exchangeself._outgoing[u].append(edge)
forself._outgoing.get(u, []).append(edge)
must solve.– Giovanni Nunes
Another thing, the function
map()
is part of the internal Python functions and it is not a good idea to use it as a variable name (map = GraphList()
). -- https://docs.python.org/3.8/library/functions.html#map– Giovanni Nunes
Fix, you need to have the key created before then use
if not self._outgoing.get(u): self.__outgoing[u]=[]
before theself._outgoing[u].append(edge)
to create it and avoid the error -- I just made the substitution without paying much attention to the rest :-)– Giovanni Nunes
@Giovanninunes, I have a csv file that represents the connections between stations and the line to which they belong in the format (station 1, station 2, line) - (1,2,1). How can I associate this key with the name of each station and thus create the respective edges? I have another file where I have the station id. Is there a way to cross all this information?
– Sandra Silva
@Giovanninunes thanks for the map function hint. I’ve changed the name :)
– Sandra Silva
@Giovanninunes my csv files have the following structure: 'Lisbon.stations.csv' -> (id, latitude, longitude, name) = (1, -9.3, 9.3, Lisbon) 'Lisbon.connections.csv' -> (station 1, station 2, line) = (1,2,1) How can I associate station 1 to the station name whose id is 1 and insert that edge?
– Sandra Silva