Hướng dẫn how do you find common letters in a string in python? - làm thế nào để bạn tìm thấy các chữ cái phổ biến trong một chuỗi trong python?

Đây là một chương trình Python để kiểm tra các chữ cái phổ biến trong hai chuỗi đầu vào.

Mô tả vấn đề

Chương trình lấy hai chuỗi và kiểm tra các chữ cái chung trong cả hai chuỗi.

Giải pháp vấn đề

1. Nhập hai chuỗi đầu vào và lưu trữ nó trong các biến riêng biệt. 2. Chuyển đổi cả hai chuỗi thành các bộ và tìm các chữ cái chung giữa cả hai bộ. 3. Lưu trữ các chữ cái phổ biến trong một danh sách. 4. Sử dụng một vòng lặp để in các chữ cái của danh sách. 5. Thoát.
2. Convert both of the strings into sets and find the common letters between both the sets.
3. Store the common letters in a list.
4. Use a for loop to print the letters of the list.
5. Exit.

Chương trình/mã nguồn

Dưới đây là mã nguồn của chương trình Python để kiểm tra các chữ cái phổ biến trong hai chuỗi đầu vào. Đầu ra chương trình cũng được hiển thị dưới đây.

s1=raw_input["Enter first string:"]
s2=raw_input["Enter second string:"]
a=list[set[s1]&set[s2]]
print["The common letters are:"]
for i in a:
    print[i]

Giải thích chương trình

1. Người dùng phải nhập hai chuỗi đầu vào và lưu trữ nó trong các biến riêng biệt. 2. Cả hai chuỗi được chuyển đổi thành các bộ và các chữ cái chung giữa cả hai bộ được tìm thấy bằng toán tử ‘&. 3. Những chữ cái phổ biến này được lưu trữ trong một danh sách. 4. A For Loop được sử dụng để in các chữ cái của danh sách.
2. Both of the strings are converted into sets and the common letters between both the sets are found using the ‘&’ operator.
3. These common letters are stored in a list.
4. A for loop is used to print the letters of the list.

Trường hợp kiểm tra thời gian chạy

 
Case 1:
Enter first string:Hello
Enter second string:How are you
The common letters are:
H
e
o
 
Case 2:
Enter first string:Test string
Enter second string:checking
The common letters are:
i
e
g
n

Sê -ri Giáo dục & Học tập toàn cầu Sanfoundry - Chương trình Python.

Để thực hành tất cả các chương trình Python, đây là bộ hoàn thành hơn 150 vấn đề và giải pháp Python.

Bước tiếp theo:

  • Nhận Giấy chứng nhận miễn phí trong chương trình Python
  • Tham gia cuộc thi chứng nhận lập trình Python
  • Trở thành một người xếp hạng hàng đầu trong chương trình Python
  • Thực hiện các bài kiểm tra lập trình Python
  • Các bài kiểm tra thực hành theo chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10
  • Các bài kiểm tra giả chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,

Manish Bhojasia, một cựu chiến binh công nghệ với hơn 20 năm @ Cisco & Wipro, là người sáng lập và CTO tại Sanfoundry. Ông sống ở Bangalore, và tập trung vào sự phát triển của nhân Linux, Công nghệ San, Cvanced C, Cấu trúc dữ liệu & Alogrithms. Giữ kết nối với anh ta tại LinkedIn.Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Đăng ký các lớp học chính miễn phí của mình tại YouTube & Thảo luận kỹ thuật tại Telegram SanfoundryClasses.

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh

    Bàn luận

    1. Đưa ra hai chuỗi, in tất cả các ký tự phổ biến theo thứ tự từ vựng. Nếu không có chữ cái chung, in -1. Tất cả các chữ cái là chữ thường. & Nbsp;Counter[str] method, which contains characters of string as key and their frequencies as value.
    2. Vấn đề này có giải pháp hiện tại Vui lòng giới thiệu các ký tự phổ biến của hai chuỗi trong liên kết thứ tự bảng chữ cái. Chúng tôi sẽ giải quyết vấn đề này trong Python bằng cách sử dụng mô -đun thuộc tính và bộ sưu tập giao nhau. Cách tiếp cận là đơn giản,intersection [ a&b ] property.
    3. Chuyển đổi cả hai chuỗi thành kiểu dữ liệu từ điển bằng phương thức bộ đếm [STR], chứa các ký tự của chuỗi là khóa và tần số của chúng là giá trị.
    4. Bây giờ tìm các phần tử phổ biến giữa hai chuỗi sử dụng thuộc tính giao lộ [A & B].elements[] method of counter dictionary to expand list of keys by their frequency number of times.
    5. Kết quả cũng sẽ là một từ điển truy cập có các yếu tố phổ biến là các khóa và tần số phổ biến của chúng là giá trị.

    Implementation:

    Python3

    from collections import Counter

    def common[str1,str2]:

     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    0
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    1
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    3

     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    0
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    5
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    7

     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    0
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    9
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2
    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    1

    ____10

    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    3
    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    4
    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    5
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2
    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    8
    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    9

    eegks
    0
    eegks
    1
    eegks
    2
    eegks
    3
    eegks
    4
    eegks
    5

    eegks
    0
    eegks
    7

    ____10

    eegks
    9
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2 from1from2

    ____10

    eegks
    9
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2 from6from7

     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    0
    eegks
    1 collections 0

    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    3 collections 2
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2 collections 5
    Input : 
    string1 : geeks
    string2 : forgeeks
    Output : eegks
    Explanation: The letters that are common between 
    the two strings are e[2 times], g[1 time], k[1 time] and 
    s[1 time].
    Hence the lexicographical output is "eegks"
    
    Input : 
    string1 : hhhhhello
    string2 : gfghhmh
    Output : hhh
    9

     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    0collections 8
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2 import0

     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    0import2
     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    2 import4

     
    Case 1:
    Enter first string:Hello
    Enter second string:How are you
    The common letters are:
    H
    e
    o
     
    Case 2:
    Enter first string:Test string
    Enter second string:checking
    The common letters are:
    i
    e
    g
    n
    0import6

    Output:

    eegks

    Độ phức tạp về thời gian: O [n] & nbsp; không gian phụ trợ: O [n]
    Auxiliary Space : O[n]


    Bài Viết Liên Quan

    Chủ Đề