2015-08-14 69 views
2

兩個文件我有以下兩個文件存在/不存在基質:使從使用python

的(a):

A01 
A02 
A03 
A10 
A11 
C03 
C10 
C11 
E01 
E10 
E11 
H01 
H02 
H10 
H11 
Y09 
Y10 
Y11 

(B):

E01 Y09 A02 
A01 A03 
C03 H01 H02 
E10 
Y10 
A10 
C10 
H10 
E11 A11 C11 H11 Y11 

我試圖從這個數據中做出一個存在/不存在的矩陣,以查看(a)中的值是否存在於(b)中的行中。如果它們存在,那麼它們應該用「1」表示,否則應該用「0」表示,其中「0」和「1」指示符跟隨(a)中的值的序列。

我的預期輸出是:

0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 
1 0 1 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 1 1 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 1 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 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 1 0 0 0 0 
0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 

我曾嘗試以下:

text_file = open("Table", "w") 
a = file("list", "r") 
b = file("cluster", "r") 

for line in a: 
    words = line.split("\n") 
for line in b: 
    words = line.split("\t") 


for line in a: 
    if words in a == words in b: 
     print("1") 
    elif words in a != words in b: 
     print("0") 
text_file.close 

然而,這並不顯示任何信息。

任何人都可以幫忙嗎?

+0

你什麼特別的意思是「存在於的行(b)中」?我在這裏很難找到模式。 – Malonge

+0

這將有助於知道什麼是行和列的名稱;) – csiu

+0

嗨@Malonge,在文件(a)中將只有一列每行一個單詞,在文件(b)將有多個列(一(b)中的每行一行),(b)中的每行將具有不同數量的列。所以,我想看看(a)中的值(每行)是否出現在(b)中的任何行中。這更清楚嗎? :) – Gloom

回答

0

這產生從兩個輸入文件所要求的輸出:

with open('list', 'rt') as list_file: 
    values = [line.strip() for line in list_file] 

with open('cluster', 'rt') as cluster_file, open('Table', 'wt') as table_file: 
    for cluster in (set(line.split()) for line in cluster_file): 
     row = ' '.join('01'[value in cluster] for value in values) 
     table_file.write(row + '\n') 
+0

非常感謝,@Martineau! – Gloom

2

我想我明白你想在這裏。

final_matrix = [] 
a = file("list", "r") 
a_list = [] 
# Make a list of all strings in the first file. 
for line in a: 
    a_list.append(line.rstrip()) 

b = file("cluster", "r") 
for line in b: 
    L1 = line.split('\t') 
    # Make a presence/absence row for each line in the second file. 
    this_row = [1 if i in L1 else 0 for i in a_list] 
    final_matrix.append(this_row) 

for row in final_matrix: 
    print row 
    # You can get fancier with this because right now it will 
    # Print these out as lists. 

在這種情況下,最終的矩陣被保存爲列表的列表。

+0

感謝你們@Malonge!我可以看到你背後的邏輯。但是,當我嘗試運行這個時,我沒有輸出;你知道這可能是爲什麼嗎? – Gloom

+0

好了,現在沒有涉及打印。只需打印出final_matrix。我會加入我的代碼來幫助你。 – Malonge

+0

非常感謝@Malonge!這正是我正在尋找的!我試圖把它保存爲一個outfile(通過在開頭添加「text_file = open(」Table「,」w「)」,並將「text_file.write(row)」和「text_file.close()」添加到文件,但是這不保存文件,你會碰巧有任何關於如何解決這個問題的建議嗎?再次感謝! – Gloom