Đếm các từ trùng lặp trong chuỗi Python

Bài viết này đề cập đến chương trình trong Python loại bỏ tất cả các từ trùng lặp khỏi chuỗi đã cho. Tất cả các từ xuất hiện nhiều lần trong một chuỗi được coi là trùng lặp ngoại trừ lần xuất hiện đầu tiên hoặc lần đầu tiên của nó

Câu hỏi đặt ra là hãy viết chương trình Python để xóa các từ trùng lặp khỏi một chuỗi. Chuỗi phải được nhập bởi người dùng trong thời gian chạy. Đây là câu trả lời của nó

print["Enter the String: "]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

print["\nNew String =", new_str]

Đây là ảnh chụp đầu ra ban đầu được tạo bởi chương trình trên

Bây giờ cung cấp đầu vào say hello how are hello are hello you và nhấn phím

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
6 để xóa tất cả các từ trùng lặp khỏi chuỗi đã cho như được hiển thị trong đầu ra mẫu được cung cấp bên dưới

Phiên bản sửa đổi của chương trình trước đó

Tôi đã nói chương trình này là phiên bản sửa đổi của chương trình trước đó, bởi vì chương trình này bao gồm một số tính năng bổ sung như hiển thị thông báo theo sự sẵn có của các từ trùng lặp. Tức là, tôi đã bao gồm ba thông báo riêng biệt trong cả ba trường hợp, chẳng hạn như khi không tìm thấy từ trùng lặp, chỉ tìm thấy 1 từ trùng lặp và tìm thấy nhiều từ trùng lặp. Hãy cùng xem qua chương trình và lần chạy mẫu của nó để hiểu một cách rõ ràng

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]

Ảnh chụp nhanh được cung cấp bên dưới hiển thị quá trình chạy mẫu của chương trình trên sau khi cung cấp đầu vào chính xác giống như lần chạy mẫu của chương trình trước đó

Hôm nay chúng ta sẽ viết một hàm python có tên là

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
7 sẽ chấp nhận một chuỗi,
print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
8, làm đối số

Bạn được cung cấp một chuỗi. Mục tiêu của hàm là trả về số ký tự phân biệt chữ hoa chữ thường [số hoặc ký tự chữ cái] xuất hiện nhiều lần trong một chuỗi

Đây là một ví dụ

duplicate_count["abcdeaa"] //output: 1
duplicate_count["abcdeaB"] // output: 2
duplicate_count["Indivisibilities"] // output: 2

Đối với ví dụ đầu tiên, chỉ có ký tự

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
9 được tìm thấy nhiều lần nên hàm sẽ trả về
duplicate_count["abcdeaa"] //output: 1
duplicate_count["abcdeaB"] // output: 2
duplicate_count["Indivisibilities"] // output: 2
0. Mặc dù có ba ký tự
print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
9 trong chuỗi, nhưng mục tiêu của hàm không phải là đếm số ký tự lặp lại mà là đếm số ký tự trùng lặp

Trong ví dụ thứ hai, cả

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
9 và
lowerText = text.lower[]
1 [nhớ viết thường không quan trọng] là những ký tự duy nhất có ký tự trùng lặp

Ví dụ thứ ba, cả chữ cái

lowerText = text.lower[]
2 và
lowerText = text.lower[]
3 đều xuất hiện nhiều lần, hàm trả về
lowerText = text.lower[]
4

Để bắt đầu hàm, vì các ký tự chữ thường và chữ hoa sẽ được xem xét theo cùng một cách, chúng tôi sẽ làm cho mọi thứ dễ dàng hơn bằng cách viết thường đầu vào chuỗi của chúng tôi

Chúng tôi gán chuỗi chữ thường cho một biến có tên là

lowerText = text.lower[]
5

lowerText = text.lower[]

Tiếp theo, chúng ta sẽ tạo một biến tên là

lowerText = text.lower[]
6 và gán cho nó một mảng trống. Chúng tôi sẽ sử dụng mảng này để giữ tất cả các ký tự trùng lặp

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
0

Điều này là do khi chúng ta lặp qua tất cả các ký tự trong đầu vào chuỗi của mình, bằng cách sử dụng phương thức

lowerText = text.lower[]
7, chúng ta có thể kiểm tra xem ký tự lặp hiện tại có trùng lặp trong chuỗi không. Miễn là chúng tôi chưa thêm ký tự đó vào mảng
lowerText = text.lower[]
6 của mình, chúng tôi có thể thêm ký tự đó vào

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
3

Sau khi chúng tôi hoàn thành việc lặp qua chuỗi, mảng

lowerText = text.lower[]
6 của chúng tôi hiện có tất cả các ký tự riêng biệt có các ký tự trùng lặp. Chúng ta có thể trả về số ký tự bằng cách trả về độ dài của mảng đó

print["Enter the String: ", end=""]
str = input[]

old_word_list = str.split[]
new_word_list = []
j = 0
count = 0

for i in range[len[old_word_list]]:
    if old_word_list[i] not in new_word_list:
        new_word_list.insert[j, old_word_list[i]]
        j = j+1
    else:
        count = count+1

new_str = ""
for i in range[len[new_word_list]]:
    new_str = new_str + new_word_list[i] + " "

if count==0:
    print["\nNo duplicate word found!"]
elif count==1:
    print["\nA single duplicate word found and removed successfully!"]
    print["\nNow the new string =", new_str]
else:
    print["\nTotal", count, "duplicate words found and removed successfully!"]
    print["\nNow the new string =", new_str]
5

Đây là phần còn lại của chức năng

Đây là bài viết thuật toán python đầu tiên nhưng sẽ có nhiều hơn nữa trong tương lai. Trong thời gian chờ đợi, nếu bạn đang muốn học JavaScript tiếp theo, hãy xem các bài viết về giải pháp thuật toán JavaScript của tôi

Chủ Đề