2015-04-17 65 views
0

datadicabst是列表,在形式的數據:[[0, 1, 0, 0 , 1...0],[1, 0, 0, 1, 0, 1....0]],在循環之後,我得到的結果是這樣的:如何單獨打印列表而不是合併它們?

abst = ['PS50802', 'PS50803', 'PS50804', 'PS50805', 'PS50806', 'PS50807', 'PS50808', 'PS50809', 'PS50810', 'PS50811', 'PS50812', 'PS50813', 'PS50814',......]

dic = ['PS50102' 'PS50101' 'PS50106' 'PS50199' 'PS50196' 'PS00832' 'PS50072'...]

[0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0]

看起來像兩個單獨的列表合併成一個列表,但是怎麼可以我得到的結果是這樣的:

[0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, ,[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, ,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0]]

lines = [] 

for x in range(0, 2): 
    for item in dic: 
     for i,j in enumerate(abst):  
      if item == j: 
       lines.append(data[x][i]) 

print lines 
+0

你能解決你的第二個結果?它缺少一個括號 –

+0

你可以發佈你的數據,dic和abst變量的簡化版本,並確切地說明你想要實現的目標嗎? – CoMartel

+0

@HarryPotfleur,是的,我做到了。 – Kun

回答

1

如果您想保留兩個列表的列表,你可以試試這個:

lines = [] 
for x in range(0, 2): 
    line = [] 
    for item in dic: 
     for i,j in enumerate(abst):  
      if item == j: 
       line.append(data[x][i]) 
    lines.append(line) 

print lines 

,你也許能夠簡化

lines = [] 
for x in range(0, 2): 
    line = [] 
    for item in dic: 
     if item in abst: 
      i = abst.index(item) 
      line.append(data[x][i]) 
    lines.append(line) 

lines = [] 
for x in range(0, 2): 
    line = [data[x][abst.index(item)] for item in dic if item in abst] 
    lines.append(line) 

最後,用一個列表理解:

lines = [ [data[x][abst.index(item)] for item in dic if item in abst] for x in range(0, 2) ] 
+0

是的,這是我想要的,謝謝。 – Kun

+0

不客氣:-) –

相關問題