Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận In this type of file, Each line of text is terminated with a special character called EOL [End of Line], which is the new line character [‘\n’] in python by default.
- Python cung cấp các chức năng sẵn có để tạo, viết và đọc các tệp. Hai loại tệp có thể được xử lý trong Python, tệp văn bản thông thường và tệp nhị phân [được viết bằng ngôn ngữ nhị phân, 0s và 1s]. In this type of file, there is no terminator for a line, and the data is stored after converting it into machine-understandable binary language.
Tệp văn bản: Trong loại tệp này, mỗi dòng văn bản được chấm dứt với một ký tự đặc biệt có tên EOL [cuối dòng], là ký tự dòng mới [‘\ n,] trong Python theo mặc định. .txt file in Python. Through this program, we will find the most repeated word in a file.
Approach:
- Tệp nhị phân: Trong loại tệp này, không có bộ hủy nào cho một dòng và dữ liệu được lưu trữ sau khi chuyển đổi nó thành ngôn ngữ nhị phân có thể hiểu bằng máy.
- Ở đây chúng tôi đang hoạt động trên tệp .txt trong Python. Thông qua chương trình này, chúng tôi sẽ tìm thấy từ lặp đi lặp lại nhất trong một tệp.
- Chúng tôi sẽ lấy nội dung của tệp làm đầu vào.
- Chúng tôi sẽ lưu từng từ trong một danh sách sau khi xóa khoảng trắng và dấu câu từ chuỗi đầu vào.
Tìm tần số của mỗi từ.
In từ có tần số tối đa.
Python3
Tệp đầu vào:
Dưới đây là việc thực hiện phương pháp trên:
file
=
open
[
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy0____11
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy2
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy3
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy4
=
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy6
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy7
=
________ 19 & nbsp;with open[inputFile, 'r'] as filedata:0____7
with open[inputFile, 'r'] as filedata:2
with open[inputFile, 'r'] as filedata:3
with open[inputFile, 'r'] as filedata:4
with open[inputFile, 'r'] as filedata:5
file
with open[inputFile, 'r'] as filedata:7
5from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
6from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
Các
with open[inputFile, 'r'] as filedata:8
with open[inputFile, 'r'] as filedata:3
2from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
with open[inputFile, 'r'] as filedata:5
4from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
with open[inputFile, 'r'] as filedata:3
8from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
with open[inputFile, 'r'] as filedata:5
{ TutorialsPoint } is the most repeated word in a text file0
[
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy9
{ TutorialsPoint } is the most repeated word in a text file3
{ TutorialsPoint } is the most repeated word in a text file4
{ TutorialsPoint } is the most repeated word in a text file5
with open[inputFile, 'r'] as filedata:8
{ TutorialsPoint } is the most repeated word in a text file7
=
{ TutorialsPoint } is the most repeated word in a text file9
file
0with open[inputFile, 'r'] as filedata:8
with open[inputFile, 'r'] as filedata:3
file
3with open[inputFile, 'r'] as filedata:5
{ TutorialsPoint } is the most repeated word in a text file0
file
.with open[inputFile, 'r'] as filedata:8
=
3open
7Các
‘
5from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy7
=
[
15from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy4
=
[
5file
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy08
Output:
Most repeated word: well Frequency: 3
Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách tìm từ lặp lại nhiều nhất trong một tệp văn bản nhất định bằng Python.
Giả sử chúng tôi đã lấy một tệp văn bản với TÊN excletExtFile.txt bao gồm một số văn bản ngẫu nhiên. Chúng tôi sẽ trả về từ lặp lại nhiều nhất trong một tệp văn bản đã choExampleTextFile.txt consisting of some random text. We will return the most repeated word in a given text file
ExampleTextFile.txt
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint Learn with a joy
Thuật toán [bước]
Sau đây là các thuật toán/các bước được tuân thủ để thực hiện nhiệm vụ mong muốn -
Nhập hàm bộ đếm [lớp bộ đếm là một hình thức của bộ dữ liệu đối tượng được cung cấp bởi mô-đun bộ sưu tập của Python3. Mô-đun bộ sưu tập hiển thị các kiểu dữ liệu container chuyên dụng cho người dùng, phục vụ như một giải pháp thay thế cho các mục đích đa năng của Python như từ điển, danh sách , và các bộ dữCounter function [The Counter class is a form of object data-set provided by Python3's collections module. The Collections module exposes specialized container datatypes to the user, serving as an alternative to Python's general-purpose built-ins such as dictionaries, lists, and tuples. The Counter is a subclass that counts hashable objects. When called, it creates an iterable hash table implicitly] from the collections module
Tạo một biến để lưu trữ đường dẫn của tệp văn bản.
Tạo một danh sách để lưu trữ tất cả các từ.
Sử dụng hàm Open [] [kết quả là mở tệp và trả về đối tượng tệp] để mở tệp văn bản ở chế độ chỉ đọc bằng cách chuyển tên tệp và chế độ làm đối số cho nó [ở đây cách thức].open[] function[opens a file and returns a file object as a result] to open the text file in read-only mode by passing the file name, and mode as arguments to it [Here “r” represents read-only mode].
with open[inputFile, 'r'] as filedata:
Traverse trong mỗi dòng của tệp bằng cách sử dụng vòng lặp.
Sử dụng hàm Split [] [chia một chuỗi vào danh sách. Chúng ta có thể xác định bộ phân cách; Bộ phân cách mặc định là bất kỳ khoảng trắng nào] để chia nội dung tệp văn bản thành một danh sách các từ và lưu trữ nó trong một biến.split[] function [splits a string into a list. We can define the separator; the default separator is any whitespace] to split the text file content into a list of words and store it in a variable.
Traverse trong danh sách các từ sử dụng vòng lặp.
Sử dụng hàm append [] [thêm phần tử vào danh sách ở cuối], để nối từng từ vào danh sách.append[] function [adds the element to the list at the end], to append each word to the list.
Sử dụng hàm bộ đếm [] [đưa ra tần số của các từ như một cặp giá trị khóa], để tính toán tần số [số lần từ đã xảy ra] của tất cả các từ.Counter[] function [which gives the frequency of words as a key-value pairs], to calculate the frequency [number of times the word has occurred] of all the words.
Tạo một biến để lưu trữ tần số tối đa.
Vòng lặp trong từ điển tần số từ trên bằng cách sử dụng vòng lặp.
Sử dụng câu lệnh IF có điều kiện và từ khóa, kiểm tra xem tần số của từ có lớn hơn tần số tối đa hay không.
The in keyword works in two ways: The in keyword is used to determine whether a value exists in a sequence [list, range, string etc]. It is also used to iterate through a sequence in a for loop
Nếu tần số của từ lớn hơn tần số tối đa.
Tạo một biến để lưu trữ từ lặp lại nhiều nhất trong một tệp văn bản.
In từ lặp lại nhiều nhất trong một tệp văn bản.
Đóng tệp đầu vào với hàm đóng [] [được sử dụng để đóng tệp đã mở].close[] function [used to close an opened file].
Thí dụ
Chương trình sau đây chuyển các dòng của tệp văn bản và in tần số của cặp giá trị khóa từ tệp văn bản bằng cách sử dụng hàm bộ đếm từ mô -đun bộ sưu tập -
from collections import Counter inputFile = "ExampleTextFile.txt" newWordsList = [] with open[inputFile, 'r'] as filedata: for textline in filedata: wordsList = textline.split[] for word in wordsList: newWordsList.append[word] wordsFrequency = Counter[newWordsList] maxFrequency = 0 for textword in wordsFrequency: if[wordsFrequency[textword] > maxFrequency]: maxFrequency = wordsFrequency[textword] mostRepeatedWord = textword print["{",mostRepeatedWord,"} is the most repeated word in a text file"] filedata.close[]
Đầu ra
Khi thực hiện, chương trình trên sẽ tạo ra đầu ra sau -
{ TutorialsPoint } is the most repeated word in a text file
Trong chương trình này, chúng tôi đọc một số văn bản ngẫu nhiên từ một tệp văn bản. Chúng tôi đọc toàn bộ tệp, chia nhỏ nó thành các từ và thêm tất cả các từ của tệp văn bản vào danh sách. Chúng tôi đã sử dụng phương thức bộ đếm [] để đếm tần số của tất cả các từ trong tệp văn bản, trả về một từ điển với các khóa là từ và giá trị là tần số của từ. Sau đó, chúng tôi lặp lại các từ của từ điển, kiểm tra xem tần số có lớn hơn tần số tối đa hay không. Nếu đó là, đây là từ thường xuyên nhất, vì vậy chúng tôi đã lưu kết quả trong một biến và cập nhật tần số tối đa với tần số của từ hiện tại. Cuối cùng, chúng tôi đã hiển thị từ thường xuyên nhất.
Sự kết luận
Bài viết này chỉ cho chúng tôi cách đọc một tập tin, đi qua từng dòng và truy xuất tất cả các từ trong dòng đó. Khi chúng tôi nhận được chúng, chúng tôi có thể đảo ngược các từ, thay đổi trường hợp, kiểm tra các nguyên âm, truy xuất độ dài từ, v.v. Chúng tôi cũng đã học cách sử dụng phương thức bộ đếm [] để xác định tần suất của danh sách các từ. Hàm này có thể được sử dụng để xác định tần số của một chuỗi, danh sách, tuple, v.v.
Cập nhật vào ngày 18 tháng 8 năm 2022 08:50:24
- Câu hỏi và câu trả lời liên quan
- Tìm từ lặp đi lặp lại thứ hai trong một chuỗi trong java
- Từ thứ hai lặp lại trong một chuỗi trong Python?
- Làm thế nào để tìm và thay thế từ trong một tệp văn bản bằng PowerShell?
- Tìm từ lặp lại đầu tiên trong một chuỗi trong Python bằng từ điển
- Tìm từ lặp lại đầu tiên trong một chuỗi trong Python?
- Làm thế nào để đếm các từ xuất hiện trong một tệp văn bản bằng cách sử dụng script shell?
- Viết một chương trình bằng Python để tìm phần tử lặp đi lặp lại nhất trong một loạt
- Làm thế nào để tìm và thay thế trong một tệp văn bản bằng Python?
- Tìm từ lặp lại đầu tiên trong một chuỗi trong java
- Tìm từ được lặp lại đầu tiên trong một chuỗi trong C ++
- Làm thế nào để tìm chuỗi lặp lại thứ hai nhiều lần trong một chuỗi trong Android?
- Làm thế nào để tìm một tệp bằng Python?
- Làm thế nào để tìm tập tin gần đây nhất trong một thư mục trên Linux?
- Làm thế nào để tìm kiếm và thay thế văn bản trong một tệp bằng Python?
- Làm thế nào để viết một dòng trong tệp văn bản bằng Python?
- Làm thế nào để đọc một tệp văn bản trong Python?