I have a question concerning some python code that I have written:
def read_graph_from_file(filename):
txtfile = open(filename, "rU")
node_memory = 0
neighbour_list = 0
for entry in txtfile:
entry_without_newline = entry.replace('\n',"")
columns = entry_without_newline.replace(','," ")
columns = columns.split(" ")
number_of_columns = len(columns)
if number_of_columns == 2:
neighbour_list = columns
neighbour_list.sort()
if node_memory == float(neighbour_list[0]):
y = neighbour_list[1]
print y
The output I want from this is a list, i.e. [1,4]. Instead I am receiving the characters across multiple lines, i.e:
1
4
I was wondering how I might rectify this?
yindividually, which will result in a new line per iteration. Instead, append to a list first, then print the list at the end.print y,to print a space instead of a newline, or accumulate a string to point out, etc. But if you want to print a list, the obvious thing to do is to create a list, as @JesseMu says.