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
Post a Comment