为什么将空列表传递给binary_search函数?毕竟,它并不是真的空荡荡的.

from typing import List


def binary_search(nums: List[int], item: int):
    print("binary_search nums", nums)
    low = 0
    high = len(nums) - 1

    while low <= high:
        middle = (low + high) // 2

        if nums[middle] == item:
            return middle
        if nums[middle] > item:
            high = middle - 1
        if nums[middle] < item:
            low = middle + 1
    return None


def smallest_find(nums: List[int]):
    smallest = nums[0]
    smallest_index = 0

    for i in range(1, len(nums)):
        if nums[i] < smallest:
            smallest = nums[i]
            smallest_index = i
    return smallest_index


def selection_sort(nums: List[int]):
    sorted_nums = []

    for _ in range(len(nums)):
        smallest_index = smallest_find(nums)
        sorted_nums.append(nums.pop(smallest_index))
    return sorted_nums

import random

while True:
    nums = [random.randint(-100, 100) for _ in range(20)]
    item = 24
    if item in nums:
        print(nums)
        print(selection_sort(nums))
        print(binary_search(selection_sort(nums), item))
        break

结果:

[20, 84, -65, -76, -46, -7, 19, 48, -55, 31, 62, -86, 16, 24, 67, -87, -65, -57, 12, 61]
[-87, -86, -76, -65, -65, -57, -55, -46, -7, 12, 16, 19, 20, 24, 31, 48, 61, 62, 67, 84]
binary_search nums []
None

为什么将空列表传递给binary_search函数?毕竟,它并不是真的空荡荡的.

Python3.8.10

推荐答案

您第二次调用selection_sort实际上会返回一个空列表,这就是为什么binary_search会得到一个空列表.

原因是这一行:

sorted_nums.append(nums.pop(smallest_index))

在你的selection_sort routine 中,你从nums开始的所有值都是pop.这是清空(并覆盖)输入列表nums

您可以通过制作nums的浅表副本来避免这种情况:

from typing import List


def binary_search(nums: List[int], item: int):
    print("binary_search nums", nums)
    low = 0
    high = len(nums) - 1

    while low <= high:
        middle = (low + high) // 2

        if nums[middle] == item:
            return middle
        if nums[middle] > item:
            high = middle - 1
        if nums[middle] < item:
            low = middle + 1
    return None


def smallest_find(nums: List[int]):
    smallest = nums[0]
    smallest_index = 0

    for i in range(1, len(nums)):
        if nums[i] < smallest:
            smallest = nums[i]
            smallest_index = i
    return smallest_index


def selection_sort(nums: List[int]):
    sorted_nums = []
    nums_ = nums[:] # shallow copy
    for _ in range(len(nums_)):
        smallest_index = smallest_find(nums_)
        sorted_nums.append(nums_.pop(smallest_index))
    return sorted_nums

import random

while True:
    nums = [random.randint(-100, 100) for _ in range(20)]
    item = 24
    if item in nums:
        print(nums)
        print(selection_sort(nums))
        print(binary_search(selection_sort(nums), item))
        break

输出:

[41, -59, 38, -50, 25, -62, 58, -81, 7, 24, -93, 41, 92, 3, 65, -61, 47, -54, 72, 65]
[-93, -81, -62, -61, -59, -54, -50, 3, 7, 24, 25, 38, 41, 41, 47, 58, 65, 65, 72, 92]
binary_search nums [-93, -81, -62, -61, -59, -54, -50, 3, 7, 24, 25, 38, 41, 41, 47, 58, 65, 65, 72, 92]
9

Python-3.x相关问答推荐

使用pybind11时,在sys.exit(0)处成功完成测试后,Python单元测试冻结

Pandas :从元组字典创建数据帧

具有多个值的极轴旋转和熔化/取消旋转(反转旋转)操作(Pandas 堆叠/取消堆叠交替/UDF覆盖)

动态范围内来自另外两列的列求和

从PYTHON中获取单行和多行的Rguar表达式

Pandas 插入的速度太慢了.对于跟踪代码,什么是更快的替代方案?

PyTest:尽管明确运行了测试,但是被标记为没有运行测试

将值从函数传递到标签

Python Regex 查找给定字符串是否遵循交替元音、辅音或辅音、元音的连续模式

如何向 scikit-learn 函数添加类型提示?

如何通过 GitLab V4 api 列出 gitlab 项目中的所有项目变量

这种类型提示有什么作用?

段落中句子的索引

删除重复项,但将值相加为一

Tkinter IntVar 返回 PY_VAR0 而不是值

Python:遍历子列表

如何在 jupyter notebook 5 中逐行分析 python 3.5 代码

try 在 Mac OS 中运行此命令pipenv install requests时出错

同步调用协程

Python 无法处理以 0 开头的数字字符串.为什么?