Trong hướng dẫn này, chúng ta sẽ học cách tìm tất cả các giá trị trùng lặp trong một chuỗi. Chúng ta có thể làm điều đó theo những cách khác nhau trong Python. Hãy khám phá chúng từng cái một.
Mục đích của chương trình chúng ta sẽ viết là tìm các ký tự trùng lặp có trong một chuỗi. Ví dụ: chúng tôi có một hướng dẫn chuỗi & nbsp; chương trình sẽ cung cấp cho chúng tôi t o i & nbsp; làm đầu ra. Nói một cách đơn giản, chúng ta phải tìm các ký tự có số lượng lớn hơn một trong chuỗi. Hãy xem nào.tutorialspoint the program will give us t o i as output. In simple words, we have to find characters whose count is greater than one in the string. Let's see.
Chương trình cào
Viết chương trình mà không sử dụng bất kỳ mô -đun. Chúng ta có thể sử dụng các phương pháp python khác nhau để đạt được mục tiêu của mình. Đầu tiên, chúng ta sẽ tìm thấy các ký tự trùng lặp của một chuỗi bằng phương thức đếm. Hãy xem thủ tục trước.
- Khởi tạo một chuỗi.
- Khởi tạo một danh sách trống
- Vòng lặp qua chuỗi.
- Kiểm tra xem tần số char lớn hơn một hoặc không sử dụng phương pháp đếm.
If greater than one check whether it's present in the list or not. If not present append to the list
- In các ký tự
Thí dụ
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]
Nếu bạn chạy chương trình trên, bạn sẽ nhận được kết quả sau.
Đầu ra
t o i
Bây giờ chúng ta sẽ tìm thấy các ký tự trùng lặp của chuỗi mà không có bất kỳ phương thức nào. Chúng tôi sẽ sử dụng cấu trúc dữ liệu từ điển để có được đầu ra mong muốn. Hãy xem thủ tục trước.
- Khởi tạo một chuỗi.
- Khởi tạo một danh sách trống
- Vòng lặp qua chuỗi.
- Kiểm tra xem tần số char lớn hơn một hoặc không sử dụng phương pháp đếm.
- In các ký tự
Increase the count
Thí dụ
## initializing string string = "tutorialspoint" ## initializing a dictionary duplicates = {} for char in string: ## checking whether the char is already present in dictionary or not if char in duplicates: ## increasing count if present duplicates[char] += 1 else: ## initializing count to 1 if not present duplicates[char] = 1 for key, value in duplicates.items[]: if value > 1: print[key, end = " "] print[]
Nếu bạn chạy chương trình trên, bạn sẽ nhận được kết quả sau.
Đầu ra
t o i
Bây giờ chúng ta sẽ tìm thấy các ký tự trùng lặp của chuỗi mà không có bất kỳ phương thức nào. Chúng tôi sẽ sử dụng cấu trúc dữ liệu từ điển để có được đầu ra mong muốn. Hãy xem thủ tục trước.
- Khởi tạo một từ điển trống
- Kiểm tra xem char đã có trong từ điển hay không
- Khởi tạo số lượng của char đến 1
- Nếu bạn chạy chương trình trên,
- Cập nhật vào ngày 27 tháng 8 năm 2019 12:37:28
- Câu hỏi và câu trả lời liên quan
- Chương trình Java để tìm tất cả các ký tự trùng lặp trong một chuỗi
- Tìm tất cả các ký tự trùng lặp từ một chuỗi bằng Python
- Chương trình Java để tìm các ký tự trùng lặp trong một chuỗi?
- Chương trình Java để tìm các ký tự trùng lặp trong một chuỗi
- Chương trình tìm chuỗi sau khi xóa các ký tự trùng lặp liên tiếp trong Python
- Chương trình tìm chuỗi sau khi xóa K ký tự trùng lặp liên tiếp trong Python
- Chương trình xóa các ký tự trùng lặp khỏi một chuỗi đã cho trong Python
- Chương trình Python để tìm các ký tự gương trong chuỗi
- C# Chương trình để xóa các ký tự trùng lặp khỏi chuỗi
- Chương trình Java để xóa các ký tự trùng lặp khỏi một chuỗi đã cho
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : hello Output : l Input : geeksforgeeeks Output : e g k s
Bàn luận
Cho một chuỗi, tìm tất cả các ký tự trùng lặp tương tự nhau. Chúng ta hãy xem ví dụ. & NBSP;
- Chúng tôi đã thảo luận về một giải pháp trong bài viết dưới đây. In tất cả các bản sao trong chuỗi đầu vào Chúng tôi có thể giải quyết vấn đề này một cách nhanh chóng bằng phương thức Python Counter []. & NBSP;
- Cách tiếp cận rất đơn giản. & NBSP;
- Tạo một từ điển bằng phương pháp bộ đếm có chuỗi làm khóa và tần số của chúng làm giá trị.
Khai báo một biến nhiệt độ.
In tất cả các chỉ mục từ các khóa có giá trị lớn hơn 1. & nbsp;
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]1
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]2
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]4
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
t o i2
t o i8
t o i4
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
Increase the count9
t o i6
t o i7
t o i8
Input : hello Output : l Input : geeksforgeeeks Output : e g k s5
t o i4
Input : hello Output : l Input : geeksforgeeeks Output : e g k s7
Input : hello Output : l Input : geeksforgeeeks Output : e g k s8
t o i7
Increase the count5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
Increase the count8
Increase the count9
Increase the count1
Increase the count2
Increase the count3
t o i7
Increase the count5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
Increase the count8
Increase the count9
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
## initializing string string = "tutorialspoint" ## initializing a dictionary duplicates = {} for char in string: ## checking whether the char is already present in dictionary or not if char in duplicates: ## increasing count if present duplicates[char] += 1 else: ## initializing count to 1 if not present duplicates[char] = 1 for key, value in duplicates.items[]: if value > 1: print[key, end = " "] print[]3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]2
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
t o i0
Cách tiếp cận: Sử dụng phương thức Count []
Python3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]1
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]2
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]4
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
t o i3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
t o i5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
t o i2
t o i8
t o i4
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
Increase the count9
t o i6
t o i7
t o i8
Input : hello Output : l Input : geeksforgeeeks Output : e g k s5
t o i4
Input : hello Output : l Input : geeksforgeeeks Output : e g k s7
Input : hello Output : l Input : geeksforgeeeks Output : e g k s8
Increase the count1
from
4## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
Increase the count2
from
7from
8from
9t o i7
Increase the count5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
Increase the count8
Increase the count9
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]7
## initializing string string = "tutorialspoint" ## initializing a dictionary duplicates = {} for char in string: ## checking whether the char is already present in dictionary or not if char in duplicates: ## increasing count if present duplicates[char] += 1 else: ## initializing count to 1 if not present duplicates[char] = 1 for key, value in duplicates.items[]: if value > 1: print[key, end = " "] print[]3
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]5
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]2
## initializing string string = "tutorialspoint" ## initializing a list to append all the duplicate characters duplicates = [] for char in string: ## checking whether the character have a duplicate or not ## str.count[char] returns the frequency of a char in the str if string.count[char] > 1: ## appending to the list if it's already not present if char not in duplicates: duplicates.append[char] print[*duplicates]3
t o i0