```lo == 0, hi == len(cards) - 1``` 和 ```lo, hi = 0, len(cards) - 1``` 有什么区别
What is the difference between ```lo == 0, hi == len(cards) - 1``` and ```lo, hi = 0, len(cards) - 1```
我目前正在练习二进制搜索,但我无法理解 lo == 0, hi == len(cards) - 1
和 lo, hi = 0, len(cards) - 1
之间的语法差异。该代码适用于第二种方法,但不适用于第一种方法。任何帮助,将不胜感激。谢谢。
代码如下:
def binary_search(cards, query):
# lo == 0, hi == len(cards) - 1
lo, hi = 0, len(cards) - 1
while lo <= hi:
mid = (lo+hi) // 2
result = locate_card(cards, query, mid)
mid_card = cards[mid]
print('lo: ', lo, 'hi: ', hi)
print('mid: ', mid)
print('mid_card: ', mid_card)
print('result: ', result)
print('\n')
if result == 'found':
return mid
elif result == 'left':
hi = mid - 1
else:
lo = mid + 1
return -1
def locate_card(cards, query, mid):
if cards[mid] == query:
if mid > 0 and cards[mid-1] == query:
return 'left'
else:
return 'found'
elif cards[mid] < query:
return 'left'
else:
return 'right'
return binary_search(cards, query)
if __name__ == '__main__':
cards = [13, 11, 10, 7, 4, 3, 1, 0]
query = 1
print(binary_search(cards, query))
双精度 equal-sign (==
) 是比较两个对象是否相等的运算符。 lo == 0, hi == len(cards) - 1
会将 lo
与 0
以及 hi
与 len(cards)-1
进行比较。
lo, hi = 0, len(cards) - 1
等同于写
lo = 0
hi = len(cards) - 1
当我们要使用条件表达式并想比较两个表达式,如果为真,则执行操作时使用==运算符,但=运算符用于为变量赋值。
例如:
if lo == 0 and hi == len(cards) - 1:
print("Hello")
我建议在 python 解释器中尝试一下,以便更好地理解三个符号 =
(赋值)、==
(相等测试)和 ,
(用于构建元组)。
玩转 ==
和 ,
:元组相等,或等式元组
>>> 3 == 3, 4 == 4
(True, True)
>>> 3, 4 == 3, 4
(3, False, 4)
>>> (3, 4) == (3, 4)
True
>>> 3 == 3 and 4 == 4
True
玩 =
和 ,
:解包赋值
>>> 0, 10
(0, 10)
>>> lo, hi = (0, 1)
>>> print(lo, hi)
0 1
>>> lo, hi = 1, 2
>>> print(lo, hi)
1 2
>>> lo = 3
>>> hi = 4
>>> print(lo, hi)
3 4
赋值元组:语法不正确
>>> lo = 5, hi = 6
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
带 :=
的赋值表达式 (python >= 3.8)
>>> (lo := 7, hi := 8)
(7, 8)
>>> print(lo, hi)
7 8
我目前正在练习二进制搜索,但我无法理解 lo == 0, hi == len(cards) - 1
和 lo, hi = 0, len(cards) - 1
之间的语法差异。该代码适用于第二种方法,但不适用于第一种方法。任何帮助,将不胜感激。谢谢。
代码如下:
def binary_search(cards, query):
# lo == 0, hi == len(cards) - 1
lo, hi = 0, len(cards) - 1
while lo <= hi:
mid = (lo+hi) // 2
result = locate_card(cards, query, mid)
mid_card = cards[mid]
print('lo: ', lo, 'hi: ', hi)
print('mid: ', mid)
print('mid_card: ', mid_card)
print('result: ', result)
print('\n')
if result == 'found':
return mid
elif result == 'left':
hi = mid - 1
else:
lo = mid + 1
return -1
def locate_card(cards, query, mid):
if cards[mid] == query:
if mid > 0 and cards[mid-1] == query:
return 'left'
else:
return 'found'
elif cards[mid] < query:
return 'left'
else:
return 'right'
return binary_search(cards, query)
if __name__ == '__main__':
cards = [13, 11, 10, 7, 4, 3, 1, 0]
query = 1
print(binary_search(cards, query))
双精度 equal-sign (==
) 是比较两个对象是否相等的运算符。 lo == 0, hi == len(cards) - 1
会将 lo
与 0
以及 hi
与 len(cards)-1
进行比较。
lo, hi = 0, len(cards) - 1
等同于写
lo = 0
hi = len(cards) - 1
当我们要使用条件表达式并想比较两个表达式,如果为真,则执行操作时使用==运算符,但=运算符用于为变量赋值。 例如:
if lo == 0 and hi == len(cards) - 1:
print("Hello")
我建议在 python 解释器中尝试一下,以便更好地理解三个符号 =
(赋值)、==
(相等测试)和 ,
(用于构建元组)。
玩转 ==
和 ,
:元组相等,或等式元组
>>> 3 == 3, 4 == 4
(True, True)
>>> 3, 4 == 3, 4
(3, False, 4)
>>> (3, 4) == (3, 4)
True
>>> 3 == 3 and 4 == 4
True
玩 =
和 ,
:解包赋值
>>> 0, 10
(0, 10)
>>> lo, hi = (0, 1)
>>> print(lo, hi)
0 1
>>> lo, hi = 1, 2
>>> print(lo, hi)
1 2
>>> lo = 3
>>> hi = 4
>>> print(lo, hi)
3 4
赋值元组:语法不正确
>>> lo = 5, hi = 6
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
带 :=
的赋值表达式 (python >= 3.8)
>>> (lo := 7, hi := 8)
(7, 8)
>>> print(lo, hi)
7 8