私は、100万文字の長さで、句読点がなく、文字の集まりだけの8つのファイルを読み取ることを目的としたプログラムを持っています。
8 つのファイルは、見つかった 4 つの DNA サンプルを表しており、プログラムが行うことは、サンプル内の 1 つのファイルから文字を取得し、同じサンプルの別のファイル内の文字と結合することです。たとえば、file1 が次のように読み取られた場合:
abcdefg
と file2 読み取り:
hijklmn
組み合わせは次のようになります。
ah, bi, cj, dk, el, fm, gn
いずれにせよ、プログラムは次に、各ペアの組み合わせがいくつ存在するかをカウントし、たとえば次のような辞書を出力します。
{'mm': 52, 'CC': 66, 'SS': 24, 'cc': 19, 'MM': 26, 'ss': 58, 'TT': 43, 'tt': 32}
問題は、プログラムは小さなファイルでは正常に動作しますが、100 万文字の長さ (はい、これはリテラル数であり、誇張ではありません) のファイルでは、プログラムがハングし、タスクを完了できないように見えることです。(私は一度一晩実行したままにしましたが、何も起こりませんでした。)
オーバーフロー エラーですか、それとも私が使用しているメソッドは大きなファイルに対して小さすぎますか? これを処理するより良い方法はありますか?
私のコード:
import re
from collections import Counter
def ListStore(fileName):
'''Purpose, stores the contents of file into a single string'''
#old code left in for now
'''
with open(fileName, "r") as fin:
fileContents = fin.read().rstrip()
fileContents = re.sub(r'\W', '', fin.read())
'''
#opens up the file given to the function
fin = open(fileName,'r')
#reads the file into a string, strips out the newlines as well
fileContents = fin.read().rstrip()
#closes up the file
fin.close()
#splits up the fileContents into a list of characters
fileContentsList = list(fileContents)
#returns the string
return fileContentsList
def ListCombo(list1, list2):
'''Purpose: combines the two DNA lists into one'''
#creates an empty dictionary for list3
list3 = []
#combines the codes from one hlaf with their matching from the other
list3 = [''.join(pair) for pair in zip(list1, list2)]
return list3
def printResult(list):
'''stores the result of the combination in a dictionary'''
#stores the result into a dictionary
result = dict((i,list.count(i)) for i in list)
print (result)
return result
def main():
'''Purpose: Reads the contents of 8 files, and finds out how many
combinations exist'''
#first sample files
file_name = "a.txt"
file_name2 = "b.txt"
#second sample files
file_name3 = "c.txt"
file_name4 = "d.txt"
#third sample files
file_name5 = "e.txt"
file_name6 = "f.txt"
#fourth sample files
file_name7 = "g.txt"
file_name8 = "h.txt"
#Get the first sample ready
#store both sides into a list of characters
contentList = ListStore(file_name)
contentList2 = ListStore(file_name2)
#combine the two lists together
combo_list = ListCombo(contentList, contentList2)
#store the first sample results into a dictionary
SampleA = printResult(combo_list)
print (SampleA)
# ****Get the second sample ready****
#store both sides into a list of characters
contentList3 = ListStore(file_name3)
contentList4 = ListStore(file_name4)
#combine the two lists together
combo_list2 = ListCombo(contentList3, contentList4)
#store the first sample results into a dictionary
SampleB = printResult(combo_list2)
print (SampleB)
# ****Get the third sample ready****
#store both sides into a list of characters
contentList5 = ListStore(file_name5)
contentList6 = ListStore(file_name6)
#combine the two lists together
combo_list3 = ListCombo(contentList5, contentList6)
#store the third sample results into a dictionary
SampleC = printResult(combo_list3)
print (SampleC)
# ****Get the second sample ready****
#store both sides into a list of characters
contentList7 = ListStore(file_name7)
contentList8 = ListStore(file_name8)
#combine the two lists together
combo_list4 = ListCombo(contentList7, contentList8)
#store the fourth sample results into a dictionary
SampleD = printResult(combo_list4)
print (SampleD)
if __name__ == '__main__':
main()