Xem thảo luận Show 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: 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.
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.
true0 true1 true2 true3 true4 true5 true0 true7 true2 true9 true4 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 true2 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 true3 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 correct0 true4 true4 The string is Hi Mark The input pattern is Ma The order of pattern is correct3 The string is Hi Mark The input pattern is Ma The order of pattern is correct4 true9 true4 true9 The string is Hi Mark The input pattern is Ma The order of pattern is correct8 The string is Hi Mark The input pattern is Ma The order of pattern is correct9 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 from 2true4 true4 from 5from 6from 7The string is Hi Mark The input pattern is Ma The order of pattern is correct4 from 9 collections 0true2 from 9 collections 3from 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 5true4 true4 collections 8collections 9true2 true0 true4 import 3Output: true true2 import 5true4 import 7Shashank 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.true2 import 9 OrderedDict 0true0 OrderedDict 2Khi đượ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 raThe string is Hi Mark The input pattern is Ma The order of pattern is correct Giải trình
Cập nhật vào ngày 17 tháng 4 năm 2021 12:40:18
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" |