Hướng dẫn python program to print duplicates from a string - chương trình python để in các bản sao từ một chuỗi

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

  • Đọc
  • Bàn luận
  • 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;

    1. 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;
    2. Cách tiếp cận rất đơn giản. & NBSP;
    3. 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 i
    2
    t o i
    8
    t o i
    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]
    3
    Increase the count
    9

    t o i
    6
    t o i
    7
    t o i
    8
    Input : hello
    Output : l
    
    Input : geeksforgeeeks
    Output : e g k s
    5
    t o i
    4
    Input : hello
    Output : l
    
    Input : geeksforgeeeks
    Output : e g k s
    7
    Input : hello
    Output : l
    
    Input : geeksforgeeeks
    Output : e g k s
    8

    t o i
    7
    Increase the count
    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]
    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 count
    8
    Increase the count
    9

    Increase the count
    1
    Increase the count
    2
    Increase the count
    3

    t o i
    7
    Increase the count
    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]
    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 count
    8
    Increase the count
    9

    ## 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 i
    0

    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 i
    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
    t o i
    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]
    5
    t o i
    2
    t o i
    8
    t o i
    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]
    3
    Increase the count
    9

    t o i
    6
    t o i
    7
    t o i
    8
    Input : hello
    Output : l
    
    Input : geeksforgeeeks
    Output : e g k s
    5
    t o i
    4
    Input : hello
    Output : l
    
    Input : geeksforgeeeks
    Output : e g k s
    7
    Input : hello
    Output : l
    
    Input : geeksforgeeeks
    Output : e g k s
    8

    Increase the count
    1from4

    ## 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 count
    2from7from8from9

    t o i
    7
    Increase the count
    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]
    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 count
    8
    Increase the count
    9

    ## 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 i
    0


    Làm cách nào để tìm thấy các bản sao trong một danh sách chuỗi trong Python?

    Nhiều cách để kiểm tra xem các bản sao có tồn tại trong danh sách Python không..
    Độ dài của danh sách & chiều dài của bộ khác nhau ..
    Kiểm tra từng phần tử trong tập. Nếu có, DUP, nếu không, hãy nối thêm ..
    Kiểm tra danh sách.Count [] cho mỗi yếu tố ..

    Làm thế nào để bạn trích xuất các bản sao từ một danh sách trong Python?

    Nếu bạn chỉ muốn trích xuất các phần tử trùng lặp từ danh sách ban đầu, hãy sử dụng Bộ sưu tập. Chuẩn bị [] trả về các bộ sưu tập. Bộ đếm [lớp con từ điển] có khóa là một phần tử và có giá trị là số lượng của nó. Vì nó là một lớp con của từ điển, bạn có thể truy xuất các khóa và giá trị bằng các mục [].use collections. Counter[] that returns collections. Counter [dictionary subclass] whose key is an element and whose value is its count. Since it is a subclass of a dictionary, you can retrieve keys and values with items[] .

    Làm thế nào để tôi tìm thấy một ký tự lặp đi lặp lại trong một chuỗi?

    Cho một chuỗi, hãy tìm ký tự lặp lại đầu tiên trong đó ...
    Sao chép mảng đã cho vào nhiệt độ mảng phụ trợ [] ..
    Sắp xếp mảng nhiệt độ bằng thuật toán sắp xếp thời gian O [n log n] ..
    Quét mảng đầu vào từ trái sang phải.Đối với mọi yếu tố, hãy đếm các sự xuất hiện của nó trong temp [] bằng cách sử dụng tìm kiếm nhị phân ..

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

    Từ menu chính, chọn mã |Phân tích mã |Xác định vị trí trùng lặp ......
    Xem danh sách các bản sao ở khung bên trái của cửa sổ công cụ ..
    Xem sự khác biệt giữa các bản sao được tìm thấy ở khung bên phải.....
    Điều hướng đến các bản sao trong trình chỉnh sửa bằng cách sử dụng các lệnh nhảy vào nguồn hoặc hiển thị các lệnh menu ngữ cảnh nguồn ..

    Bài Viết Liên Quan

    Chủ Đề