THISfile.py라는 이름의 파일에 아래의 코드를 넣고 그것이 무엇인지보고 실행을 수행합니다
는
# myFile = input("Enter file name: ")
# line No 2: line with with double 'with'
# line No 3: double (word , word) is not a double word
myFile="THISfile.py"
lstUniqueWords = []
noOfFoundWordDoubles = 0
totalNoOfWords = 0
lineNo = 0
lstLineNumbersWithWordDoubles = []
with open(myFile, "r") as myFile:
for line in myFile:
lineNo+=1 # memorize current line number
lineWords = line.split()
if len(lineWords) > 0: # scan line only if it contains words
currWord = lineWords[0] # remember already 'visited' word
totalNoOfWords += 1
if currWord not in lstUniqueWords:
lstUniqueWords.append(currWord)
# put 'visited' word word into lstAllWordsINmyFile (if it is not already there)
lastWord = currWord # we are done with current, so current becomes last one
if len(lineWords) > 1 : # proceed only if line has two or more words
for word in lineWords[1:] : # loop over all other words
totalNoOfWords += 1
currWord = word
if currWord not in lstUniqueWords:
lstUniqueWords.append(currWord)
# put 'visited' word into lstAllWordsINmyFile (if it is not already there)
if(currWord == lastWord): # duplicate word found:
noOfFoundWordDoubles += 1
print("Found double word: ['{""}'] in line {}".format(currWord, lineNo))
lstLineNumbersWithWordDoubles.append(lineNo)
lastWord = currWord
# ^--- now after all all work is done, the currWord is considered lastWord
print(
"noOfDoubles", noOfFoundWordDoubles, "\n",
"totalNoOfWords", totalNoOfWords, "uniqueWords", len(lstUniqueWords), "\n",
"linesWithDoubles", lstLineNumbersWithWordDoubles
)
는 출력은 다음과 같아야합니다
Found double word: ['with'] in line 2
Found double word: ['word'] in line 19
Found double word: ['all'] in line 33
noOfDoubles 3
totalNoOfWords 221 uniqueWords 111
linesWithDoubles [2, 19, 33]
지금 코드의 주석을 확인하여 작동 방식을 더 잘 이해할 수 있습니다. 재미 코딩 적이 :
그냥'LST를 다시 = []'각 라인의 반복에서. –
@ Jean-FrançoisFabre는 인접한 단어뿐만 아니라 줄에 중복 된 단어를 감지합니다. – Maciek