【发布时间】:2019-11-20 07:06:02
【问题描述】:
我想创建一个元组列表并拥有这张表:
Nr Name Value F/R
1 6347_sx 123.98 F
2 hff_475 234.99 F
3 sjdh_65 123.67 R
4 6347_sx 345.12 R
我想要一个这样的列表:
norm_list = [('6347_sx',123.98), ('hff_475',234.99), ('sjdh_65',123.67), ('6347_sx',345.12)]
我尝试了这个,但它没有给我想要的输出:
norm_file = open("table.txt")
norm_list = []
for norm_line in norm_file.readlines():
norm_file_elements = norm_line.split()
x = norm_file_elements[1]
y = norm_file_elements[2]
norm_list= [(x,y)]
print(norm_list)
y 值必须是 int。 感谢您的帮助。
【问题讨论】:
-
norm_list.append((x,y))
-
norm_list = [tuple(line.split()[1:3]) for line in norm_file] -
我希望 y 值为 int。我怎样才能做到这一点?
标签: python python-3.x list tuples