gpt4 book ai didi

python - 错误代码 : UnboundLocalError on line 10

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:46 24 4
gpt4 key购买 nike

下面的 python 代码的第 10 行有一个 UnboundLocalError。谁能教我如何解决这个问题吗?

def answer(data, n):
new_data = []
for each_integer in data:
new_data = [each_integer for each_integer in data if data.count(each_integer) == n]
if n > 1:
new_data = data
print("\n\nNew Data")
print(new_data)

supplied_data = [53, 85, 29, 23, 29, 26, 88, 78, 5, 75, 74, 44, 33, 62, 98, 50, 89, 93, 24, 14, 74, 49, 83, 45, 41, 14, 68, 76, 68, 8, 77, 85, 17, 3, 9, 30, 71, 48, 18, 25, 86, 55, 55, 20, 74, 76, 99, 87, 59, 87, 36, 29, 29, 8, 22, 65, 1, 18, 23, 5, 13, 60, 7, 5, 98, 61, 78, 64, 36, 60, 49, 57, 31, 32, 41, 86, 52, 90, 9, 55, 35, 35, 2, 44, 8, 19, 96, 81, 68, 7, 8, 51, 9, 76, 12, 96, 61, 99, 74]
answer(supplied_data, 0)
answer(supplied_data, 1)
answer(supplied_data, 6)

回溯

    >>> def answer(data, n):    ...     for each_integer in data:    ...         new_data = [each_integer for each_integer in data if data.count(each_integer) == n]    ...     if n > 1:    ...         new_data = data    ...     print("\n\nNew Data")    ...     print(new_data)    ...    ... supplied_data = [53, 85, 29, 23, 29, 26, 88, 78, 5, 75, 74, 44, 33, 62, 98, 50, 89, 93, 24, 14, 74, 49, 83, 45, 41, 14, 68, 76, 68, 8, 77, 85, 17, 3, 9, 30, 71, 48, 18, 25, 86, 55, 55, 20,     74, 76, 99, 87, 59, 87, 36, 29, 29, 8, 22, 65, 1, 18, 23, 5, 13, 60, 7, 5, 98, 61, 78, 64, 36, 60, 49, 57, 31, 32, 41, 86, 52, 90, 9, 55, 35, 35, 2, 44, 8, 19, 96, 81, 68, 7, 8, 51, 9, 76, 12    , 96, 61, 99, 74]      File "", line 9        supplied_data = [53, 85, 29, 23, 29, 26, 88, 78, 5, 75, 74, 44, 33, 62, 98, 50, 89, 93, 24, 14, 74, 49, 83, 45, 41, 14, 68, 76, 68, 8, 77, 85, 17, 3, 9, 30, 71, 48, 18, 25, 86, 55, 55, 20,     74, 76, 99, 87, 59, 87, 36, 29, 29, 8, 22, 65, 1, 18, 23, 5, 13, 60, 7, 5, 98, 61, 78, 64, 36, 60, 49, 57, 31, 32, 41, 86, 52, 90, 9, 55, 35, 35, 2, 44, 8, 19, 96, 81, 68, 7, 8, 51, 9, 76, 12    , 96, 61, 99, 74]                    ^    SyntaxError: invalid syntax    >>> answer(supplied_data, 0)    Traceback (most recent call last):      File "", line 1, in     NameError: name 'answer' is not defined    >>> answer(supplied_data, 1)    Traceback (most recent call last):      File "", line 1, in     NameError: name 'answer' is not defined    >>> answer(supplied_data, 6)    Traceback (most recent call last):      File "", line 1, in     NameError: name 'answer' is not defined    >>>

最佳答案

问题是您的逻辑中有一个使用 new_data 的分支但有可能new_data从未被定义。当 data 时会发生这种情况为空,因此您永远不会进入 for 循环体并创建 new_data ,和n <= 1 ,即您输入 else-block ,您使用 new_data没有被定义。

参见:

>>> answer([],1)


New Data
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 10, in answer
UnboundLocalError: local variable 'new_data' referenced before assignment

您可以通过输入new_data = data来快速解决这个问题在你的函数的顶部。老实说,这种方法效率确实很低,因为它的工作时间是二次方的。 some_list.count每次都会迭代整个列表!因此,最好先进行一次计数元素,然后再进行一次过滤 - 这将是线性时间:

>>> from collections import Counter
>>> def answer(data, n):
... counts = Counter(data)
... return [e for e in data if counts[e] == n]
...
>>> answer(data, 6)
[]
>>> answer(data, 2)
[85, 23, 78, 44, 98, 14, 49, 41, 14, 85, 18, 86, 99, 87, 87, 36, 18, 23, 60, 7, 98, 61, 78, 36, 60, 49, 41, 86, 35, 35, 44, 96, 7, 96, 61, 99]
>>> answer(data, 3)
[5, 68, 76, 68, 9, 55, 55, 76, 5, 5, 9, 55, 68, 9, 76]
>>> answer(data, 4)
[29, 29, 74, 74, 8, 74, 29, 29, 8, 8, 8, 74]
>>> answer(data, 5)

关于python - 错误代码 : UnboundLocalError on line 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41682870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com