Reputation: 519
I am coding a python program related to graphs.
My main is like this
if __name__=='__main__':
cns = [(0,1), (0,2),(1,2), (1,3),(3,1)]
G=make_graph(cns)
r=DFS(G)
I want to change the program such that the user can input the data.
cns = [(0,1), (0,2),(1,2), (1,3),(3,1)]
this list is to be read from the user. How to input a list of tuples, how to do that?
Can I use raw_input
for this purpose?
Upvotes: 2
Views: 3325
Reputation: 87
I would like to add to kindall's answer. When you run kindall's code, you are asked to enter the data. To enter a tuple, you can enter (1,2,3). Now to check if it's actually a tuple, you can add 2 lines: cns[0]=8 print cns
The output will give some error: "TypeError: 'tuple' object does not support item assignment".
Then, you know that cns is a tuple. Thanks @kindall!
Upvotes: 0
Reputation: 184201
from ast import literal_eval
cns = literal_eval(raw_input("Please enter the data: "))
Upvotes: 5
Reputation: 113988
points = map(lambda x:map(float,x.split(",")),
iter(lambda:raw_input("Enter X,Y coordinates or Nothing to continue").strip(),""))
print points
Upvotes: 0