Bạn có thể so sánh bảng chữ cái trong python không?

ID đối tượng của các chuỗi có thể khác nhau trên các máy khác nhau. ID đối tượng của str1, str2 và str3 giống nhau nên kết quả là True trong mọi trường hợp. Sau khi id đối tượng của str1 bị thay đổi, kết quả của str1 và str2 sẽ là false. Ngay cả sau khi tạo str4 với cùng nội dung như trong str1 mới, câu trả lời sẽ sai vì ID đối tượng của chúng khác nhau

Ngược lại sẽ xảy ra với is not

Phương pháp 3. Tạo một hàm do người dùng định nghĩa

Bằng cách sử dụng các toán tử quan hệ, chúng ta chỉ có thể so sánh các chuỗi theo unicodes của chúng. Để so sánh hai chuỗi theo một số tham số khác, chúng ta có thể tạo các hàm do người dùng định nghĩa

Trong đoạn mã sau, hàm do người dùng xác định của chúng tôi sẽ so sánh các chuỗi dựa trên số chữ số

Python3




ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
04

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
05

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
06
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
07

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
08
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
09
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
6
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
11

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
08
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
13____26
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
11

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
16

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
08
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
18
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
19
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
20
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
21
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
8
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
23
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
24

ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
25
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
26
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
51
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
6
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
29
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
30
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
55
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
6
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
33
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True

ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
34

Chúng ta có thể so sánh bảng chữ cái trong Python không?

Bạn có thể sử dụng [ > , < ,

Chủ Đề