我有以下文件txt:
0 1
0 2
0 3
1 637
1 754
我需要将其加载为具有以下格式的列表:
list = [("0","1"), ("0","2"), ("0","3"), ("1","637"), ("1","754")]
我该怎么办? 感谢
答案 0 :(得分:1)
你可以这样试试,
>>> [ (item.split()[0], item.split()[1]) for item in open("your_file.txt").readlines() ]
[('0', '1'), ('0', '2'), ('0', '3'), ('1', '637'), ('1', '754')]
答案 1 :(得分:1)
with open("input.txt") as f:
print([tuple(line.split()) for line in f])
[('0', '1'), ('0', '2'), ('0', '3'), ('1', '637'), ('1', '754')]