简体   繁体   中英

This program is taking way too long to run

This code is taking a really long time to run.

It is meant to find the most common word in a text file.

It is in Python 3.5.1.

The Code:

cTR = []
cTRC = []


file = open("fileToCompress3.txt","r")
analysis = file.read().split(' ')
file.close()


##### Most Common Word #####
print(1)

for i in range(len(analysis)):

    if analysis[i] not in cTR:
        cTR.extend([analysis[i]])
        cTRC.extend([1])

    elif analysis[i] in cTR:

        for j in range(len(cTR)):

            if cTR[j] == analysis[i]:

                use = j
                break

        cTRC[use] = cTRC[use] + 1

Thank You and I really appreciate any help!

Yep, that's a really inefficient way to do that – ϴ(n²) time. Python has a built-in counter type that does ϴ(n) comparisons:

from collections import Counter

with open("fileToCompress3.txt", "r", encoding="utf-8") as f:
    words = f.read().split()

word_counts = Counter(words)

print(word_counts.most_common(1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM