Hướng dẫn write a python program to check order of character in string using ordereddict()? - viết một chương trình python để kiểm tra thứ tự của ký tự trong chuỗi sử dụng lệnhdict ()?

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: 
    string = "engineers rock"
    pattern = "er";
    Output: true
    Explanation: 
    All 'e' in the input string are before all 'r'.
    
    
    Input: 
    string = "engineers rock"
    pattern = "gsr";
    Output: false
    Explanation:
    There are one 'r' before 's' in the input string.
    

    Bàn luận{IDE} first, before moving on to the solution.

    Đưa ra một chuỗi đầu vào và một mẫu, hãy kiểm tra xem các ký tự trong chuỗi đầu vào tuân theo cùng thứ tự như được xác định bởi các ký tự có trong mẫu. Giả sử có chiến thắng là bất kỳ ký tự trùng lặp trong mẫu.

    • Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp.Key only.
    • Chúng tôi có giải pháp hiện tại cho vấn đề này, vui lòng tham khảo Kiểm tra xem chuỗi theo thứ tự của các ký tự được xác định bởi một mẫu hoặc không | Đặt 1. Ở đây chúng tôi giải quyết vấn đề này một cách nhanh chóng trong Python bằng cách sử dụng OrderedDict (). Cách tiếp cận rất đơn giản,
    • Tạo một thứ tự của chuỗi đầu vào chứa các ký tự của chuỗi đầu vào chỉ là khóa.
    • Bây giờ đặt một con trỏ ở đầu chuỗi mẫu.

    Bây giờ Traverse đã tạo ra OrderedDict và khớp các phím với ký tự riêng lẻ của chuỗi mẫu, nếu khóa và ký tự khớp với nhau thì tăng con trỏ thêm 1.

    Nếu con trỏ của mẫu đạt đến đầu cuối có nghĩa là chuỗi theo thứ tự các ký tự được xác định bởi một mẫu khác thì không.

    from collections import OrderedDict 

    def checkOrder(

    true
    
    0
    true
    
    1

    true
    
    2
    true
    
    3
    true
    
    4
    true
    
    5
    true
    
    0
    true
    
    7

    true
    
    2
    true
    
    9
    true
    
    4
    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    1

    true
    
    2
    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    3
    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    4
    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    5
    true
    
    3
    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    7

    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    8
    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    9
    The string is
    Hi Mark
    The input pattern is
    Ma
    The order of pattern is correct
    0
    true
    
    4
    true
    
    4
    The string is
    Hi Mark
    The input pattern is
    Ma
    The order of pattern is correct
    3

    The string is
    Hi Mark
    The input pattern is
    Ma
    The order of pattern is correct
    4
    true
    
    9
    true
    
    4
    true
    
    9
    The string is
    Hi Mark
    The input pattern is
    Ma
    The order of pattern is correct
    8
    The string is
    Hi Mark
    The input pattern is
    Ma
    The order of pattern is correct
    9

    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    8
    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    9 from2
    true
    
    4
    true
    
    4 from5from6from7

    The string is
    Hi Mark
    The input pattern is
    Ma
    The order of pattern is correct
    4from9 collections 0

    true
    
    2from9 collections 3

    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))
    9 collections 5
    true
    
    4
    true
    
    4 collections 8collections 9

    true
    
    2
    true
    
    0
    true
    
    4 import3

    Output:

    true
    

    true
    
    2import5
    true
    
    4 import7Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    true
    
    2import9 OrderedDict 0
    true
    
    0OrderedDict 2



    Khi được yêu cầu kiểm tra thứ tự của ký tự trong chuỗi, phương thức ‘đặt hàng có thể được sử dụng.

    Dưới đây là trình diễn của cùng -

    Thí dụ

    & nbsp; bản demo trực tiếp

    from collections import OrderedDict
    def check_order(my_input, my_pattern):
       my_dict = OrderedDict.fromkeys(my_input)
       pattern_length = 0
       for key,value in my_dict.items():
          if (key == my_pattern[pattern_length]):
             pattern_length = pattern_length + 1
    
          if (pattern_length == (len(my_pattern))):
             return 'The order of pattern is correct'
    
       return 'The order of pattern is incorrect'
    
    my_input = 'Hi Mark'
    input_pattern = 'Ma'
    print("The string is ")
    print(my_input)
    print("The input pattern is ")
    print(input_pattern)
    print(check_order(my_input,input_pattern))

    Đầu ra

    The string is
    Hi Mark
    The input pattern is
    Ma
    The order of pattern is correct

    Giải trình

    • Các gói cần thiết được nhập khẩu.

    • Một phương thức có tên ‘Check_order, được xác định, có hai tham số.

    • Một từ điển được đặt hàng được tạo bằng phương pháp ‘FromKeys.

    • Độ dài của mẫu được khởi tạo thành 0.

    • Nếu khóa bằng với mẫu, thì độ dài của mẫu được tăng lên.

    • Nếu độ dài của mẫu giống như độ dài hiện tại, điều đó có nghĩa là thứ tự là đúng, nếu không thứ tự là sai.

    • Tin nhắn có liên quan được hiển thị dưới dạng đầu ra trên bảng điều khiển.

    Hướng dẫn write a python program to check order of character in string using ordereddict()? - viết một chương trình python để kiểm tra thứ tự của ký tự trong chuỗi sử dụng lệnhdict ()?

    Cập nhật vào ngày 17 tháng 4 năm 2021 12:40:18

    • Câu hỏi và câu trả lời liên quan
    • Đặt hàng trong Python
    • Nhân vật không lặp lại của K hèth trong Python bằng cách sử dụng danh sách hiểu và đặt hàng
    • Chèn vào đầu để đặt hàng bằng cách sử dụng python
    • Kiểm tra xem tần số của ký tự trong một chuỗi là một yếu tố hoặc nhiều tần số của cùng một ký tự trong chuỗi khác trong python
    • Chương trình Java để kiểm tra thứ tự của các ký tự trong chuỗi
    • Kiểm tra xem một chuỗi hai ký tự có thể được thực hiện bằng cách sử dụng các từ đã cho trong Python không
    • Tần số của từng ký tự trong chuỗi trong Python
    • Kiểm tra xem các ký tự của một chuỗi đã cho có theo thứ tự bảng chữ cái trong Python không
    • Chương trình Java để kiểm tra sự xuất hiện của từng ký tự trong chuỗi
    • Chương trình Java để kiểm tra sự xuất hiện của từng ký tự trong chuỗi
    • Kiểm tra xem ký tự trung bình của chuỗi có mặt hay không trong Python
    • Làm thế nào để đặt hàng theo chuỗi ký tự 2 cuối cùng trong MySQL?
    • Đếm sự xuất hiện của một ký tự trong chuỗi trong Python
    • Chương trình kiểm tra xem một chuỗi có chứa bất kỳ ký tự đặc biệt nào trong Python không
    • Kiểm tra xem ký tự tối đa xảy ra của một chuỗi có thể giống nhau không. của những lần khác trong Python

    Làm cách nào để tìm thứ tự các ký tự trong một chuỗi trong Python?

    Khoa học dữ liệu thực tế bằng cách sử dụng Python Khi cần phải kiểm tra thứ tự của ký tự trong chuỗi, phương thức 'OrderedDict' có thể được sử dụng.the 'OrderedDict' method can be used.

    Làm thế nào để bạn có được ký tự đầu tiên của một chuỗi trong Python?

    Có thể truy cập các ký tự riêng lẻ trong chuỗi trong một chuỗi bằng cách chỉ định tên chuỗi theo sau là một số trong dấu ngoặc vuông ([]).Lập chỉ mục chuỗi trong Python dựa trên 0: ký tự đầu tiên trong chuỗi có chỉ mục 0, tiếp theo có chỉ mục 1, v.v.specifying the string name followed by a number in square brackets ( [] ). String indexing in Python is zero-based: the first character in the string has index 0 , the next has index 1 , and so on.

    Làm cách nào để in một ký tự cụ thể trong một chuỗi trong Python?

    Tất cả những gì bạn cần làm là thêm dấu ngoặc với số char vào cuối tên của chuỗi bạn muốn in, tức là lưu câu trả lời này.Hiển thị hoạt động trên bài viết này.Tốt nếu bạn biết nhân vật bạn muốn tìm kiếm, bạn có thể sử dụng phương pháp này.add brackets with the char number to the end of the name of the string you want to print, i.e. Save this answer. Show activity on this post. Well if you know the character you want to search you can use this approach.

    Chuỗi trong Python với ví dụ là gì?

    Một chuỗi là một loạt các ký tự.Trong Python, bất cứ điều gì bên trong trích dẫn là một chuỗi.Và bạn có thể sử dụng trích dẫn đơn hoặc đôi.Ví dụ: message = 'Đây là một chuỗi trong python' message = "Đây cũng là một chuỗi"a series of characters. In Python, anything inside quotes is a string. And you can use either single or double quotes. For example: message = 'This is a string in Python' message = "This is also a string"