我想创建一个元组列表并具有此表:

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。
感谢您的任何帮助。

最佳答案

尝试zip功能

这很容易

print(list(zip(norm_file_elements[1], norm_file_elements[2]))

关于python - Python:创建元组列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58948566/

10-11 22:07
查看更多