I have class which holds a an integer and another list holds the data.
class Vertex:
def __init__(self, ID = str()):
self.id = ID
self.neighbors = AdjacencyList()
Adjacency list is another class. which holds a list.
I would to print the id and the list in the neighbors with a single print statement. How it’s possible?
This is the code I use right now (employing string concatenation)
def printGraph(g = Graph()):
msg = str('')
for k, v in g.nodes.iteritems():
root_node = g.nodes[k]
msg = "(" + k + " , " + str(root_node.color) + ") : "
for x in v.neighbors.innerlist:
msg += str(x.id) + " , "
print msg
You can see the whole string separators I used in the print method.
You can use
str.format()function to format your output: –This will work in
Python 2.6+. For older version, you might need to use the one in the @Daniel’s answer.From
Python 2.7, you can also omit that positional argument and just use{}: –