ValueError: need more than 0 values to unpack (python lists)

后端 未结 3 1388
清酒与你
清酒与你 2021-02-18 15:20

I\'m learning python from Google code class. I\'m trying out the exercises.

def front_x(words):
  x_list, ord_list = []
  for word in words:
    if word[0] == \'         


        
相关标签:
3条回答
  • 2021-02-18 15:39

    You are trying to use tuple assignment:

    x_list, ord_list = []
    

    you probably meant to use multiple assignment:

    x_list = ord_list = []
    

    which will not do what you expect it to; use the following instead:

    x_list, ord_list = [], []
    

    or, best still:

    x_list = []
    ord_list = []
    

    When using a comma-separated list of variable names, Python expects there to be a sequence of expressions on the right-hand side that matches the number variables; the following would be legal too:

    two_lists = ([], [])
    x_list, ord_list = two_lists
    

    This is called tuple unpacking. If, on the other hand, you tried to use multiple assignment with one empty list literal (x_list = ord_list = []) then both x_list and ord_list would be pointing to the same list and any changes made through one variable will be visible on the other variable:

    >>> x_list = ord_list = []
    >>> x_list.append(1)
    >>> x_list
    [1]
    >>> ord_list
    [1]
    

    Better keep things crystal clear and use two separate assignments, giving each variable their own empty list.

    0 讨论(0)
  • 2021-02-18 15:55

    return type of function does not match with values expected in function...

    check the number of variables returned from function and variables you are expecting

    0 讨论(0)
  • 2021-02-18 16:02

    Change the line

    x_list, ord_list = []
    
    to
    
    x_list, ord_list = [], []
    
    0 讨论(0)
提交回复
热议问题