So sánh các chuỗi theo cách vô cảm có vẻ tầm thường, nhưng không phải vậy. Tôi sẽ sử dụng Python 3, vì Python 2 kém phát triển ở đây. Điều đầu tiên cần lưu ý là các chuyển đổi loại bỏ trường hợp trong Unicode không phải là tầm thường. Có văn bản mà text.lower() != text.upper().lower() , chẳng hạn như "ß" : >>> "ß".lower()
'ß'
>>> "ß".upper().lower()
'ss'
Nhưng giả sử bạn muốn so sánh một cách vô tình "BUSSE" và >>> "ê" == "ê"
False
0. Heck, có lẽ bạn cũng muốn so sánh "BUSSE" và >>> "ê" == "ê"
False
2 bằng nhau - đó là hình thức vốn mới hơn. Cách được đề xuất là sử dụng >>> "ê" == "ê"
False
3:str.casefold()casefold() Trả về một bản sao được giới thiệu của chuỗi. Các chuỗi casefold có thể được sử dụng để kết hợp không đồng ý. Casfolding tương tự như LowerCasing nhưng tích cực hơn vì nó nhằm loại bỏ tất cả các trường hợp phân biệt trong một chuỗi. [...]
Không chỉ sử dụng >>> "ê" == "ê"
False
4. Nếu >>> "ê" == "ê"
False
3 không có sẵn, hãy làm >>> "ê" == "ê"
False
6 giúp (nhưng chỉ phần nào).Sau đó, bạn nên xem xét các điểm nhấn. Nếu trình kết xuất phông chữ của bạn tốt, bạn có thể nghĩ >>> "ê" == "ê"
False
7 - nhưng nó không:>>> "ê" == "ê"
False
Điều này là do giọng sau là một nhân vật kết hợp. >>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
Cách đơn giản nhất để đối phó với điều này là >>> "ê" == "ê"
False
8. Bạn có thể muốn sử dụng chuẩn hóa NFKD, nhưng hãy kiểm tra tài liệu. Sau đó, một người làmNFKD normalization, but feel free to check the documentation. Then one does>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
Để kết thúc, ở đây điều này được thể hiện trong các chức năng: import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
Ví dụ 4: & nbsp; so sánh bằng cách sử dụng caseprint () Phương thức caseprint () hoạt động tương tự như phương thức thấp hơn (). Nhưng so với phương thức thấp hơn (), nó thực hiện so sánh chuỗi nghiêm ngặt bằng cách loại bỏ tất cả các phân biệt trường hợp có trong chuỗi. Trong tiếng Đức, ‘tương đương với SS SS. Nhưng mọi người dùng có thể không biết tiếng Đức, vì vậy phương thức caseprint () chuyển đổi chữ cái tiếng Đức ‘β, thành SS, trong khi chúng ta không thể chuyển đổi chữ cái tiếng Đức‘ β thành ‘ss, bằng cách sử dụng phương thức thấp hơn (). Trong ví dụ này, chúng tôi đang kiểm tra xem lớp học của chúng tôi có có trong danh sách các lớp học hay không. Các Python3>>> "ê" == "ê"
False
9 >>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
0Các text.lower() != text.upper().lower() 3
>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
4>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 >>> "ê" == "ê"
False
40text.lower() != text.upper().lower() 3
>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
8 >>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
9import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
0 import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
1"BUSSE" 4
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
3 import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
4>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
7>>> "ê" == "ê"
False
00import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
9 Laptop is not present 0>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
1Laptop is not present 2Laptop is not present 3Đầu ra
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
3 >>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
0>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
1Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 2Classroom you are searching is present 3Laptop is not present 2Laptop is not present 3>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
1Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 9Classroom you are searching is present 3Ví dụ 2: Chuyển đổi thành chữ hoa để so sánh Trong ví dụ này, chuỗi người dùng và từng mục danh sách được chuyển đổi thành chữ hoa và sau đó so sánh được thực hiện. & NBSP; Python3>>> "ê" == "ê"
False
9 >>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
0Các text.lower() != text.upper().lower() 3
>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
4>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 "ß" 8text.lower() != text.upper().lower() 3
>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
8 >>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
9import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
0 import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
1"BUSSE" 4
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
3 "BUSSE" 6>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 "BUSSE" 9>>> "ê" == "ê"
False
00import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
9 Laptop is not present 0text.lower() != text.upper().lower() 3
Laptop is not present 2Laptop is not present 3"BUSSE" 4
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
9 Laptop is not present 6import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
3 >>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
0text.lower() != text.upper().lower() 3
Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 2Classroom you are searching is present 3Laptop is not present 2Laptop is not present 3text.lower() != text.upper().lower() 3
Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 9Classroom you are searching is present 3Ví dụ 3: & nbsp; Trong ví dụ này, chuỗi không có trong danh sách. Vì vậy, tìm kiếm không nhạy cảm trường hợp cũng trả về sai.false. Python3>>> "ê" == "ê"
False
9 >>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
0Các text.lower() != text.upper().lower() 3
>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
4>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 "ß" 8text.lower() != text.upper().lower() 3
>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
8 >>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
9import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
0 import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
1"BUSSE" 4
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
3 "BUSSE" 6>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 "BUSSE" 9>>> "ê" == "ê"
False
00import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
9 Laptop is not present 0text.lower() != text.upper().lower() 3
Laptop is not present 2Laptop is not present 3"BUSSE" 4
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
9 Laptop is not present 6import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
3 >>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
0text.lower() != text.upper().lower() 3
Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 2Classroom you are searching is present 3Laptop is not present 2Laptop is not present 3text.lower() != text.upper().lower() 3
Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 9Classroom you are searching is present 3text.lower() != text.upper().lower() 3Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 9Classroom you are searching is present 3
Laptop is not present Ví dụ 3: & nbsp; Trong ví dụ này, chuỗi không có trong danh sách. Vì vậy, tìm kiếm không nhạy cảm trường hợp cũng trả về sai.β‘ is equivalent to “ss“. But every user might not know German, so casefold() method converts German letter ‘β’ to ‘ss’ whereas we cannot convert German letter ‘β’ to ‘ss’ by using lower() method. text.lower() != text.upper().lower() 3
>>> unicodedata.normalize("NFKD", "ê") == unicodedata.normalize("NFKD", "ê")
True
4>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 >>> "ê" == "ê"
False
40Python3"BUSSE" 4
import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
3 import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
4>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3 import unicodedata
def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
def caseless_equal(left, right):
return normalize_caseless(left) == normalize_caseless(right)
7>>> "ê" == "ê"
False
88>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
3>>> "ê" == "ê"
False
90Đầu ra Ví dụ 4: & nbsp; so sánh bằng cách sử dụng caseprint () Phương thức caseprint () hoạt động tương tự như phương thức thấp hơn (). Nhưng so với phương thức thấp hơn (), nó thực hiện so sánh chuỗi nghiêm ngặt bằng cách loại bỏ tất cả các phân biệt trường hợp có trong chuỗi. Trong tiếng Đức, ‘tương đương với SS SS. Nhưng mọi người dùng có thể không biết tiếng Đức, vì vậy phương thức caseprint () chuyển đổi chữ cái tiếng Đức ‘β, thành SS, trong khi chúng ta không thể chuyển đổi chữ cái tiếng Đức‘ β thành ‘ss, bằng cách sử dụng phương thức thấp hơn (). Trong ví dụ này, chúng tôi đang kiểm tra xem lớp học của chúng tôi có có trong danh sách các lớp học hay không. >>> "ê" == "ê"
False
93Laptop is not present 2Laptop is not present 3Các >>> "ê" == "ê"
False
9 >>> "ê" == "ê"
False
92>>> "ê" == "ê"
False
93Classroom you are searching is present 0Classroom you are searching is present 1>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
18Classroom you are searching is present 3Laptop is not present 2Laptop is not present 3>>> "ê" == "ê"
False
93Classroom you are searching is present 0Classroom you are searching is present 1>>> import unicodedata
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E WITH CIRCUMFLEX']
>>> [unicodedata.name(char) for char in "ê"]
['LATIN SMALL LETTER E', 'COMBINING CIRCUMFLEX ACCENT']
25Classroom you are searching is present 3text.lower() != text.upper().lower() 3Classroom you are searching is present 0Classroom you are searching is present 1Classroom you are searching is present 9Classroom you are searching is present 3
Classroom you are searching is present Ví dụ 3: & nbsp;
Đang so sánh trường hợp chuỗi
toán tử khác với so sánh chuỗi bằng chuỗi. Phương pháp so sánh và so sánh (chuỗi, chuỗi). Tất cả họ thực hiện một so sánh nhạy cảm trường hợp.They all perform a case-sensitive comparison.
Python có cho phép bạn so sánh các chuỗi không?
Python bao gồm một số toán tử so sánh có thể được sử dụng để so sánh các chuỗi. Các toán tử này cho phép bạn kiểm tra cách các chuỗi so sánh với nhau và trả về giá trị đúng hoặc sai dựa trên kết quả.. These operators allow you to check how strings compare to each other, and return a True or False value based on the outcome.
So sánh các chuỗi trong Python là gì?
So sánh chuỗi Python được thực hiện bằng cách sử dụng các ký tự trong cả hai chuỗi.Các ký tự trong cả hai chuỗi được so sánh từng cái một.Khi các ký tự khác nhau được tìm thấy thì giá trị unicode của chúng được so sánh.Ký tự có giá trị unicode thấp hơn được coi là nhỏ hơn.performed using the characters in both strings. The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared. The character with lower Unicode value is considered to be smaller.
Python có phải là trường hợp không
Vâng, Python là một ngôn ngữ nhạy cảm trường hợp, tức là, nó xử lý các ký tự chữ hoa và chữ thường khác nhau.Điều này áp dụng cho số nhận dạng quá.Bạn phải tránh sử dụng cùng tên với các trường hợp khác nhau trong khi đặt tên định danh.Nó luôn luôn tốt để sử dụng những cái tên dễ hiểu hơn., i.e., it treats uppercase and lowercase characters differently. This applies to identifiers too. You must avoid using the same name with different cases while naming identifiers. It is always good to use names that are more easily understood. |