Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Python类型错误:不可损坏类型:';列表';_Python - Fatal编程技术网

为什么Python类型错误:不可损坏类型:';列表';

为什么Python类型错误:不可损坏类型:';列表';,python,Python,有人能告诉我为什么我得到一个TypeError:unhabable type:“list”错误在下面的打印语句中吗?出于某种原因,它不喜欢我输入函数的数据列表 from random import randint from functools import lru_cache data = [] [data.append(randint(1,1000000)) for i in range(1,1000000)] data = sorted(data) low = 0 high = l

有人能告诉我为什么我得到一个TypeError:unhabable type:“list”错误在下面的打印语句中吗?出于某种原因,它不喜欢我输入函数的数据列表

from random import randint
from functools import lru_cache


data = []

[data.append(randint(1,1000000)) for i in range(1,1000000)]

data = sorted(data)

low = 0
high = len(data) + 1


target = randint(1,100000)

print(low, high, target)

@lru_cache(maxsize = 1000)
def binary_search(data, target, low, high):
    if low > high:
        return False
    
    else:
        mid = (low + high) // 2
        if target == data[mid]:
            return True
        elif target < data[mid]:
            return binary_search(data, target, low, mid+1)
        else:
            return binary_search(data, target, mid+1, high)

print(binary_search(data, target, low, high))
来自随机导入randint
从functools导入lru\U缓存
数据=[]
[data.append(randint(11000000))用于范围内的i(11000000)]
数据=已排序(数据)
低=0
高=透镜(数据)+1
目标=randint(1100000)
打印(低、高、目标)
@lru_缓存(最大大小=1000)
def二进制搜索(数据、目标、低、高):
如果低>高:
返回错误
其他:
中=(低+高)//2
如果目标==数据[mid]:
返回真值
elif目标<数据[mid]:
返回二进制搜索(数据、目标、低、中+1)
其他:
返回二进制搜索(数据、目标、中+1、高)
打印(二进制搜索(数据、目标、低、高))

lru\u缓存
要求参数可以散列:

In [1]: from functools import lru_cache                                                                                                                                                                                                                                                  

In [2]: @lru_cache(maxsize=1000) 
   ...: def f(arg): 
   ...:     print(arg) 
   ...:                                                                                                                                                                                                                                                                                  

In [3]: f([1,2,3])                                                                                                                                                                                                                                                                       
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-816cce84b257> in <module>
----> 1 f([1,2,3])

TypeError: unhashable type: 'list'

应该有效。

这两种打印语句似乎都不会导致错误。您可以发布您得到的整个错误和堆栈跟踪吗?通常,当您试图使用列表作为键访问字典时,会引发该错误,您可以使用元组来解决它。是的!这起作用了。非常感谢,巴库留。
print(binary_search(tuple(data), target, low, high))