如何连接索引和索引并使其成为新列表 - python

时间:2018-05-10 18:55:11

标签: python python-2.7

我有这样的代码:

x = []
for fitur in self.fiturs:
    x.append(fitur[0])
a = [x , rpxy_list]
join = zip(*a)
print join

并且在self.fiturs中:

F1,1,1,1,1,0,1,1,0,0,1
F2,1,0,0,0,0,0,1,0,1,1
F3,1,0,0,0,0,0,1,1,1,1
F4,1,0,0,0,0,0,1,1,1,0
F5,14,24,22,22,22,16,18,19,26,22
F6,8.0625,6.2,6.2609,6.6818,6.2174,6.3333,7.85,6.0833,6.9655,6.9167
F7,0,0,0,0,0,0,1,0,1,0
F8,1,0,2,0,0,0,2,0,0,0
F9,1,0,0,0,1,1,0,0,0,0
F10,8,4,3,3,3,6,8,5,8,4
F11,0,0,1,0,0,1,0,0,0,0
F12,1,0,0,0,1,0,1,1,1,1

rpxt_list 是浮动

,程序的输出是:

C:\Users\USER\PycharmProjects\Skripsi\venv\Scripts\python.exe C:/Users/USER/PycharmProjects/Skripsi/coba.py
[('F1', 0.2182178902359924), ('F1', 0.2182178902359924), ('F2', 0.408248290463863), ('F3', 0.2), ('F4', 0.408248290463863), ('F5', 0.37142857142857144), ('F6', 0.5053765608632352), ('F7', 0.5), ('F8', 0.6201736729460423), ('F9', 0.2182178902359924), ('F10', 0.6864064729836441), ('F11', 0.5), ('F12', 0.0), ('F13', 0), ('F14', 0), ('F15', 0), ('F16', 0), ('F17', 0), ('F18', 0), ('F19', 0), ('F20', 0), ('F21', 0), ('F22', 0), ('F23', 0.2672612419124244), ('F24', 0.4364357804719848), ('F25', 0), ('F26', 0), ('F27', 0), ('F28', 0), ('F29', 0), ('F30', 0), ('F31', 0), ('F32', 0), ('F33', 0), ('F34', 0), ('F35', 0), ('F36', 0), ('F37', 0.7808688094430304)]

Process finished with exit code 0

我只想要这样的输出:

['F1', 0.2182178902359924]
['F2', 0.408248290463863]
etc

我的代码怎么办?

2 个答案:

答案 0 :(得分:0)

zip函数返回元组的迭代器,这就是为什么你得到元组而不是列表。如果您只想将输出作为列表,那么为什么不打印一个并省略连接线?

x = []
for fitur in self.fiturs:
    x.append(fitur[0])
a = [x , rpxy_list]
print(a)

答案 1 :(得分:0)

join = [('F1', 0.2182178902359924), ('F1', 0.2182178902359924), ('F2', 0.408248290463863), ('F3', 0.2), ('F4', 0.408248290463863), ('F5', 0.37142857142857144), ('F6', 0.5053765608632352), ('F7', 0.5), ('F8', 0.6201736729460423), ('F9', 0.2182178902359924), ('F10', 0.6864064729836441), ('F11', 0.5), ('F12', 0.0), ('F13', 0), ('F14', 0), ('F15', 0), ('F16', 0), ('F17', 0), ('F18', 0), ('F19', 0), ('F20', 0), ('F21', 0), ('F22', 0), ('F23', 0.2672612419124244), ('F24', 0.4364357804719848), ('F25', 0), ('F26', 0), ('F27', 0), ('F28', 0), ('F29', 0), ('F30', 0), ('F31', 0), ('F32', 0), ('F33', 0), ('F34', 0), ('F35', 0), ('F36', 0), ('F37', 0.7808688094430304)]
with open('filename.csv','wb') as file:
    for pair in join:
        file.write(pair)
        file.write('\n')

因为您使用'join'作为变量名,所以方法连接将被覆盖,并且不能在您的代码中使用,除非您再次删除变量'join':

del join