AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 76925751
Accepted
Sam Flynn
Sam Flynn
Asked: 2023-08-18 08:08:47 +0800 CST2023-08-18 08:08:47 +0800 CST 2023-08-18 08:08:47 +0800 CST

为什么空列表也能起作用?

  • 772

为什么向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函数传递一个空列表?毕竟,它并不是真正的空。

Python 3.8.10

python-3.x
  • 1 1 个回答
  • 37 Views

1 个回答

  • Voted
  1. Best Answer
    PatioFurnitureIsCool
    2023-08-18T08:33:55+08:002023-08-18T08:33:55+08:00

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

    原因是这一行:

    sorted_nums.append(nums.pop(smallest_index))
    

    你正在从日常工作中pop汲取所有价值观。这是清空(并覆盖)输入列表numsselection_sortnums

    您可以通过制作以下内容的浅拷贝来避免这种情况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
    
    • 1

相关问题

  • Python-Django 设置 Pandas DataFrame 的多索引不会分组/合并最后一个索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve