python- 字符串相等和 ord() 比较?
python- string equality and ord() comparison?
s1 = request.args.get('s1', '')
s2 = request.args.get('s2', '')
if '' not in [s1, s2]:
if s1 == s2:
if all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2)):
msg = "first"
else:
msg += "second"
else:
msg = "thrid"
else:
msg = 'fourth'
我要打印这段代码"second"。
我尝试了这些输入
s1 = ".0" 和 s2 = "0.00"
谁能简单解释一下“if all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2)):
”的确切含义?
我知道它与字符串相等性以及比较字符串上的 ord() 相关,我想知道它们有何不同。
在此先感谢您的帮助。
PS : 请原谅缩进。 Python这里是初学者!
没有 100% 确定的方法可以让那段代码打印第二个,因为实习是一个实现细节。
CPython 在 [-5, 256]
范围内实习整数,
所以你需要一个字符,当它传递给 ord
returns something > 256.
>>> s1 = "asdሴ"
>>> s2 = "asdሴ"
>>> s1 == s2
True
>>> all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2))
False
all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2))
检查每个 ord(c1)
是否与 ord(c2)
.
具有相同的 id
来自id
documentation:
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
CPython implementation detail: This is the address of the object in memory.
在简单的英语中,if all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2))
表示 ord(s1[i])
和 ord(s2[i])
的所有值 i
范围从 0
到 len(s1 or s2)
(其中 c1
和 c2
如果 ord(s1[i])
和 ord(s2[i])
的引用)对于两个列表是相同的
检查 zip()
文件。根据文档:
zip() returns a list of tuples, where the i-th tuple contains the i-th
element from each of the argument sequences or iterables. The returned
list is truncated in length to the length of the shortest argument
sequence
例如:
>>> l1 = [1, 2 ,3]
>>> l2 = [7, 8, 9]
>>> zip(l1, l2)
[(1, 7), (2, 8), (3, 9)]
现在[ord(c1) is ord(c2) for c1, c2 in zip(s1, s2)]
会return根据条件ord(c1) is ord(c2)
得到True/False
值的元组列表,其中c1
和c2
是tuple
来自上一个元组列表的对 return 编辑 zip()
.
现在是最后一部分。如果 all()
将 return True
如果上述 True/False
值的列表 [ ... ]
将具有 True
的所有值。如果任何单个项目为 False
,all()
将 return 的值为 False
s1 = request.args.get('s1', '')
s2 = request.args.get('s2', '')
if '' not in [s1, s2]:
if s1 == s2:
if all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2)):
msg = "first"
else:
msg += "second"
else:
msg = "thrid"
else:
msg = 'fourth'
我要打印这段代码"second"。
我尝试了这些输入 s1 = ".0" 和 s2 = "0.00"
谁能简单解释一下“if all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2)):
”的确切含义?
我知道它与字符串相等性以及比较字符串上的 ord() 相关,我想知道它们有何不同。
在此先感谢您的帮助。
PS : 请原谅缩进。 Python这里是初学者!
没有 100% 确定的方法可以让那段代码打印第二个,因为实习是一个实现细节。
CPython 在 [-5, 256]
范围内实习整数,
所以你需要一个字符,当它传递给 ord
returns something > 256.
>>> s1 = "asdሴ"
>>> s2 = "asdሴ"
>>> s1 == s2
True
>>> all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2))
False
all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2))
检查每个 ord(c1)
是否与 ord(c2)
.
id
来自id
documentation:
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
CPython implementation detail: This is the address of the object in memory.
在简单的英语中,if all(ord(c1) is ord(c2) for c1, c2 in zip(s1, s2))
表示 ord(s1[i])
和 ord(s2[i])
的所有值 i
范围从 0
到 len(s1 or s2)
(其中 c1
和 c2
如果 ord(s1[i])
和 ord(s2[i])
的引用)对于两个列表是相同的
检查 zip()
文件。根据文档:
zip() returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence
例如:
>>> l1 = [1, 2 ,3]
>>> l2 = [7, 8, 9]
>>> zip(l1, l2)
[(1, 7), (2, 8), (3, 9)]
现在[ord(c1) is ord(c2) for c1, c2 in zip(s1, s2)]
会return根据条件ord(c1) is ord(c2)
得到True/False
值的元组列表,其中c1
和c2
是tuple
来自上一个元组列表的对 return 编辑 zip()
.
现在是最后一部分。如果 all()
将 return True
如果上述 True/False
值的列表 [ ... ]
将具有 True
的所有值。如果任何单个项目为 False
,all()
将 return 的值为 False