2016-11-20 29 views
0

我正在尋找一種有效的方法來匹配兩個列表中的哈希值,並打印出匹配項和文件的內容應該是針對數據庫林所有的想法,任何人都可以推薦一個方法來驗證這些對數據庫「Python」比較兩個列表,並在列表包含「'時尋找匹配項」(鍵值)

data_base=['9d377b10ce778c4938b3c7e2c63a229a : contraband_file1.jpg', '6bbaa34b19edd6c6fa06cccf29b33125 : contraband_file2.jpg', 'e4e7c3451a35944ca8697f9f2ac037f1 : contraband_file3.jpg', '1d6d9c72e3476d336e657b50a77aee05 : contraband_file4.gif'] 

hashed_files= ['6e3b028cc1972f6ad534b9fc1428eef6 : big.jpg', 'c9505a624181faa4be86cfe2af4b71eb : msc_asdf_logo.jpg', '6bbaa34b19edd6c6fa06cccf29b33125 : nothingtoseehere.docx', 'e4e7c3451a35944ca8697f9f2ac037f1 : StarWarsReview.docx', '0e89978428a4fe88fab1be364d998b57 : wargames.jpg'] 
+1

你創建自己的數據結構?它們看起來有點奇怪,因爲它們太接近可能被重新組織成字典的東西。你有任何控制如何創建這些? – idjaw

+0

對於stackoverflow和洞佈局來說,新的數據來自許多腳本是很困難的。該數據庫由我們的講師提供,完全如此。然後我必須從下載的文件目錄創建另一個列表,將它們散列並存儲到列表中。我的目標是現在比較散列的文件與數據庫,並標記如果匹配,文件應該是 –

回答

0

使用字典和列表迭代搜索:??

data_base = {x.split(' : ')[0] : x.split(' : ')[1] for x in data_base} 
hashed_files = {x.split(' : ')[1] : x.split(' : ')[0] for x in hashed_files} 
matches = [] 
for file in hashed_files: 
    if hashed_files[file] in data_base: 
     matches.append((file, data_base[hashed_files[file]])) 

結果與

>>> matches 
[('StarWarsReview.docx', 'contraband_file3.jpg'), ('nothingtoseehere.docx', 'contraband_file2.jpg')] 
+0

我已經複製並將此代碼粘貼到一個新的腳本,一切都完美,因爲我想要一些微小的修改。但在我試圖實現它的代碼仍然有一些問題,並沒有工作,但這是一個很大的幫助,它給了從 –

0

我會用這個蟒蛇名單和字典解析:

list_split1 = [s.split(' : ') for s in data_base] 
list_split2 = [s.split(' : ') for s in hashed_files] 
data_base_dict = {k:v for k,v in list_split1} 
hashed_files_dict = {k:v for v,k in list_split2} 
for f, h in hashed_files_dict.items(): #for python3.x --- for python2.x use .iteritems() 
    if h in data_base_dict: 
     print(f, data_base_dict[h]) 

這導致:

StarWarsReview.docx contraband_file3.jpg 
nothingtoseehere.docx contraband_file2.jpg 
+0

工作的基線驚人的,你們真棒,這一個完美的作品我的代碼,雖然它需要一些調整,因爲它有一些問題,當我嘗試返回(f,data_base_dict [h])而不是打印,但這是出我想要的:) –

相關問題