누군가이 Python 코드를 더 잘 설명 할 수 있습니까? 나는 내가 할 수있는 언급,하지만 확실하지 않은 권리를 메신저 시도한이누군가이 Python 코드를 설명 할 수 있습니까?
from collections import Counter
ignore = ['the','a','if','in','it','of','or', 'to','for','is','and','are']
file = raw_input("choose a file: ")
filename = "" + file + ".txt"
def main():
with open(filename, 'r') as f:
p = f.read() # the variable p contains the contents of the file
words = p.split() #split the text file into individual words (strings)
wordCount = len(words) #count length of string, how many times does a specific string appear
print "The total number of words in the text file are:", wordCount
# Grab the top N words as input
counted_words = Counter(words)
for word in ignore: #check strings for any of the ignore words.
del counted_words[word] #delete words from
defined_words = int(raw_input("How many words do you want?")) #define amount of words to show
for word, count in counted_words.most_common(defined_words): #parse and store the most commmon words from the file
print word, count #print output
main()
을 worksw 방법에 대한 흥미있어, 파이썬에 대해 좀 더 배우려고 노력하고있어 나는?
더 구체적으로 설명해야 할 부분은 무엇입니까? –
이 코드는 실제로 작동하지 않습니다. 들여 쓰기가 깨졌습니다. Python은 실행하려고하면'IndentationError'를 발생시킵니다. – user2357112