Robert Marshall Robert Marshall - 5 months ago 37
Python Question

"Python" comparing two lists and looking for a match when a list contains '':"(key values)

Im looking for an efficient way to match the hashes in both lists and print out what the match was and what the file should be against the database? Im all out of ideas, can anyone recommend a way to validated these against a database?

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']


Using dictionaries and list iteration search:

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]]))

Results with

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