python - How to convert a list of tuples to a raw list in a more optimized manner -


here text file, consists of tuple on each line:

(1, 2) (3, 4) (5, 6) 

what's both rough , optimized perspective read above file , generate list below structure:

[[1,2],[3,4],[5,6]] 

here current approach, not want:

with open("agentlistfile.txt") f:         agentlist = [agentlist.rstrip('\n') line in f.readlines()] 

you can use ast.literal_eval safely evaluate tuple , convert tuples list inside list-comp, eg:

import ast open("agentlistfile.txt") f:     agent_list = [list(ast.literal_eval(line)) line in f] 

for more information, read doc of ast.literal_eval, , this thread.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -