ErrorCode:第10行的UnboundLocalError

时间:2017-01-16 18:30:24

标签: python

下面的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
    >>>

1 个答案:

答案 0 :(得分:1)

问题是您的逻辑中有一个使用new_data的分支,但有可能永远不会定义new_data。当data为空时会发生这种情况,因此您永远不会输入for-loop正文并创建new_datan <= 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每次都遍历整个列表!因此,最好将一个传递给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)