我有一个字符串列表,如下所示
['40799\t1890\t11-16-15\n', '40800\t1890\t11-17-15\n', '40801\t1890\t11-18-15\n', '40802\t1890\t11-19-15\n']
想要转换如下
[('40799',1890,'11-16-15'), ('40800',1890,'11-17-15'), ('40801',1890,'11-18-15'), ('40802',1890,'11-19-15')]
Tab是每列的分隔符。我能够使用一些循环转换。但是想知道是否可以使用单个命令转换列表,因为列表可能包含大量项目
答案 0 :(得分:4)
UpdatedBy
答案 1 :(得分:3)
lst = ['40799\t1890\t11-16-15\n', '40800\t1890\t11-17-15\n', '40801\t1890\t11-18-15\n', '40802\t1890\t11-19-15\n']
print [tuple(s.split()) for s in lst]
<强>输出:强>
[('40799', '1890', '11-16-15'), ('40800', '1890', '11-17-15'), ('40801', '1890', '11-18-15'), ('40802', '1890', '11-19-15')]
答案 2 :(得分:1)
>>> data = ['40799\t1890\t11-16-15\n', '40800\t1890\t11-17-15\n', '40801\t1890\t11-18-15\n', '40802\t1890\t11-19-15\n']
>>> [tuple(line.split()) for line in data]
[('40799', '1890', '11-16-15'), ('40800', '1890', '11-17-15'), ('40801', '1890', '11-18-15'), ('40802', '1890', '11-19-15')]