![](/img/trans.png)
[英]Finding median of two sorted arrays. Can some inequality checks be eliminated?
[英]Finding point of inequality in two arrays
我正在嘗試創建一個程序,該程序返回 Python 中兩個列表之間不等式的第一個點的索引。 我寫的代碼是
` a0: List[int] = [1, 2]
a1: List[int] = [1, 2, 3, 4, 5]
a2: List[int] = [1, 2, 33, 4, 5]
def diff_index(l1:list, l2: list):
if len(l1) < len(l2):
smallest = len(l1)
else:
smallest = len(l2)
i = 0
j = -1
while i < smallest:
if l1[i] != l2[i]:
j = i
break
else:
i += 1
return j
`
這不適用於上面的列表。 我無法理解我的代碼有什么問題,因為它沒有返回任何內容。
這是一種更 Pythonic 的方法:
def find_first_diff(a_list, another_list):
for i, (left, right) in zip(a_list, another_list):
if left != right:
return i
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.