Hướng dẫn how do you find duplicate words in a text file python? - làm thế nào để bạn tìm thấy các từ trùng lặp trong một tệp văn bản python?

Mã được cắt giảm này không sử dụng tệp, nhưng thật dễ dàng để kiểm tra và nghiên cứu. Sự khác biệt chính là bạn phải tải tệp và đọc cho mỗi dòng như bạn đã làm trong ví dụ của mình

example_file = """
This is a text file example

Let's see how many time example is typed.

"""
result = {}
words = example_file.split()
for word in words:
    # if the word is not in the result dictionary, the default value is 0 + 1
    result[word] = result.get(word, 0) + 1
for word, occurence in result.items():
    print("word:%s; occurence:%s" % (word, occurence))

UPDATE:

Theo đề xuất của @khachik, một giải pháp tốt hơn là sử dụng

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
8.

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]

Lặp lại thông qua các từ của mỗi dòng bằng cách sử dụng một vòng lặp. Kiểm tra xem từ hiện tại có nằm trong tập đầu tiên hay không. Nếu có, hãy thêm nó vào bộ thứ hai vì nó là một từ trùng lặp. Nếu nó không được tìm thấy, hãy thêm nó vào bộ đầu tiên vì điều này chưa được tìm thấy trước đây. , Bên trong cho vòng lặp lặp qua các từ của mỗi dòng. Đối với mỗi từ, nó kiểm tra xem nó có trong words_set hay không. Nếu có, nó sẽ thêm từ đó để trùng lặp_set vì nó là một bản sao. Khác, nó thêm nó vào Words_set., Vòng lặp được lặp qua các dòng trong danh sách và nhận các từ trong mỗi dòng bằng cách sử dụng split ()., Lặp đi qua các từ của mỗi dòng bằng cách sử dụng một vòng lặp. Kiểm tra xem từ hiện tại có nằm trong tập đầu tiên hay không.

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)

hello world
hello universe
hello again
hello world!!

words_dict = {}

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word not in words_dict:
   words_dict[word] = 0
words_dict[word] += 1

for word, count in words_dict.items():
   if count > 1:
   print(word)


Gợi ý: 2

Điều này cung cấp một số đoạn mã như:

found_words = {}
# empty dictionary
words1 = open("words1.txt", "rt").read().split(' ') # TODO - handle punctuation
for word in words1:
   if word in found_words:
   print(word + " already in file")
else :
   found_words[word] = True # could be set to anything

Bạn cũng có thể muốn theo dõi các vị trí trước đó, một cái gì đó như thế này:

with open(fname) as fh:
   vocab = {}
for i, line in enumerate(fh):
   words = line.split()
for j, word in enumerate(words):
   if word in vocab:
   locations = vocab[word]
print word "occurs at", locations
locations.append((i, j))
else :
   vocab[word] = [(i, j)]
# print "First occurrence of", word

Mã được cắt giảm này không sử dụng tệp, nhưng thật dễ dàng để kiểm tra và nghiên cứu. Sự khác biệt chính là bạn phải tải tệp và đọc cho mỗi dòng như bạn đã làm trong ví dụ của mình

example_file = ""
"
This is a text file example

Let 's see how many time example is typed.

""
"
result = {}
words = example_file.split()
for word in words:
   #
if the word is not in the result dictionary, the
default value is 0 + 1
result[word] = result.get(word, 0) + 1
for word, occurence in result.items():
   print("word:%s; occurence:%s" % (word, occurence))

Theo đề xuất của @khachik, một giải pháp tốt hơn là sử dụng

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
8.

>>> # Find the ten most common words in Hamlet >>>
   import re >>>
   words = re.findall(r '\w+', open('hamlet.txt').read().lower()) >>>
   Counter(words).most_common(10)[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
      ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]

Theo tuyến đường của bạn, bạn có thể làm điều này:

with open('tyger.txt', 'r') as f:
   lines = (f.read()).split()
for word in lines:
   if lines.count(word) > 1:
   print(f "{word}: True")
else :
   print(f "{word}: Flase")

2._

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
0

Bạn cũng có thể đếm từng từ:

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
1

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
0

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
3


Gợi ý: 3

Cập nhật lần cuối: 25 tháng 10 năm 2021, Cổng CS 2021 Giáo trình

Output:

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
4


Gợi ý: 4

Bạn có thể sử dụng các bộ để theo dõi các mục đã thấy và AnyCodings_Loops trùng lặp:, Làm thế nào để một thông báo sau khi tải lên hàng loạt tệp, đã giải quyết !!! Tôi có thể đưa ra lời giải thích anycodings_loops với chương trình làm việc, tôi có một tệp trên máy tính của mình có chứa một bài thơ thực sự dài của loop_for-loop và tôi muốn xem liệu có bất kỳ từ nào được sao chép trên mỗi dòng Nó được chia bằng dấu câu).

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
5

Bạn có thể sử dụng các bộ để theo dõi các mục đã thấy và AnyCodings_Loops trùng lặp:anycodings_loops duplicates:

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
6

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
7

sam.txt

Xin chào, đây là ngôi sao xin chào dữ liệu là anycodings_loops xin chào để bạn có thể chuyển đến xin chàoanycodings_loops Hello so you can move to the hello

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
8

Output:

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
9

1._

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
0

để chia tay, chỉ cần chúng tôi

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
1

Cho tệp loại này;

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
2

Điều này sẽ kiểm tra toàn bộ bài thơ;

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
3

Output:

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
4


Gợi ý: 5

Để tìm các từ trùng lặp từ chuỗi, trước tiên chúng tôi chia chuỗi thành các từ. Chúng tôi đếm sự xuất hiện của mỗi từ trong chuỗi. Nếu số lượng lớn hơn 1, nó ngụ ý rằng một từ đã trùng lặp trong chuỗi. , Chuyển đổi chuỗi thành chữ thường để làm cho sự so sánh không nhạy cảm., Trong chương trình này, chúng ta cần tìm ra các từ trùng lặp có trong chuỗi và hiển thị các từ đó., Sau vòng lặp bên trong, nếu số lượng của một từ lớn hơn 1 biểu thị rằng từ có bản sao trong chuỗi.

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
5

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
6

words_set = set()
duplicate_set = set()

with open('input.txt') as input_file:
   file_content = input_file.readlines()

for lines in file_content:
   words = lines.split()
for word in words:
   if word in words_set:
   duplicate_set.add(word)
else :
   words_set.add(word)

for word in duplicate_set:
   print(word)
7


Làm cách nào để tìm các từ trùng lặp trong một tệp văn bản?

Tìm bản sao trong từ một tài liệu..
Đầu tiên, nhấp vào tab Trang chủ và chọn tính năng Tìm và thay thế. ....
Nhập các nội dung được tìm thấy trong hộp tìm thấy những gì đầu vào. ....
Khi nội dung văn bản đã được nhập, chúng ta có thể nhấp vào Đọc tô sáng và chọn tô sáng tất cả để tìm tất cả các bản sao trong tài liệu ..

Làm thế nào để bạn tìm thấy các từ trùng lặp trong Python?

Cách tiếp cận là đơn giản,..
Chuỗi phân tách đầu tiên được phân tách bằng không gian ..
Bây giờ chuyển đổi danh sách các từ thành từ điển bằng cách sử dụng các bộ sưu tập. Phương pháp bộ đếm (iterator). Từ điển chứa các từ là khóa và tần số của nó là giá trị ..
Bây giờ, danh sách các từ truyền lại và kiểm tra từ đầu tiên có tần số lớn hơn 1 ..

Làm cách nào để tìm một từ lặp lại trong một chuỗi?

Algorithm..
Xác định một chuỗi ..
Chuyển đổi chuỗi thành chữ thường để làm cho so sánh không nhạy cảm ..
Chia chuỗi thành từ ..
Hai vòng sẽ được sử dụng để tìm các từ trùng lặp.....
Nếu tìm thấy một trận đấu, thì hãy tăng số lượng 1 và đặt các bản sao của Word thành '0' để tránh đếm lại ..

Làm thế nào để bạn tìm thấy các từ trùng lặp trong một đoạn văn trong java?

ALGORITHM..
Bước 1: Bắt đầu ..
Bước 2: Xác định chuỗi chuỗi = "Big Bug Bit bit một con chó đen lớn trên chiếc mũi màu đen lớn của mình".
Bước 3: Xác định số lượng ..
Bước 4: Chuyển đổi chuỗi thành trường hợp thấp hơn ..
Bước 5: Khởi tạo các từ [] để phân chia chuỗi ..
Bước 6: In "Các từ trùng lặp trong một chuỗi đã cho:".
Bước 7: Đặt I = 0.....
Bước 8: Đặt số lượng = 1 ..