Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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 使用for循环从列表列表中返回特定列表_Python_List_Python 3.x_For Loop - Fatal编程技术网

Python 使用for循环从列表列表中返回特定列表

Python 使用for循环从列表列表中返回特定列表,python,list,python-3.x,for-loop,Python,List,Python 3.x,For Loop,我想使用for循环在列表列表中查找列表 为什么它返回列表0而不是列表2 def make_str_from_row(board, row_index): """ (list of list of str, int) -> str Return the characters from the row of the board with index row_index as a single string. >>> make_str_fro

我想使用for循环在列表列表中查找列表

为什么它返回列表0而不是列表2

def make_str_from_row(board, row_index):
    """ (list of list of str, int) -> str

    Return the characters from the row of the board with index row_index
    as a single string.

    >>> make_str_from_row([['H', 'O', 'U', 'S', 'E'], ['B', 'E', 'D'], ['C', 'H', 'E', 'E', 'S', 'E']], 2)
    'CHEESE'
    """

    letter = ''
    line = ''

    for row_index in board:
        for letter in row_index:
            line = line + letter
        return line

make_str_from_row([['H', 'O', 'U', 'S', 'E'], ['B', 'E', 'D'], ['C', 'H', 'E', 'E', 'S', 'E']], 2)

行索引
不是您想象的那样。在Python中,您使用的for循环语法会导致控制变量将元素存储在集合中(在此上下文中)。换句话说,您正在迭代
board
中的每个子列表,其中对于每个迭代,
行索引
将更新为指向下一个子列表。 您需要的是单个访问,然后是内部for循环:

sub_list = board[row_index]
    for letter in sub_list:
        line = line + letter
    return line
以下是等效的:

    for letter in board[row_index]:
        line = line + letter
    return line

另一个注释,因为您的函数返回一个给定的子列表的级联字符串版本,您可以考虑一行解决方案…

返回“”。加入(线路板[行索引])

…将字符串中的字符连接到
线路板[行索引]

join
被认为比
+=
更有效,因为它避免了子字符串的浪费构造和丢弃。(为每个
+=
创建一个完全不同的字符串)特别是对于长字符串,
join
是一个很好的解决方案。

row\u index
不是您想象的那样。在Python中,您使用的for循环语法会导致控制变量将元素存储在集合中(在此上下文中)。换句话说,您正在迭代
board
中的每个子列表,其中对于每个迭代,
行索引
将更新为指向下一个子列表。 您需要的是单个访问,然后是内部for循环:

sub_list = board[row_index]
    for letter in sub_list:
        line = line + letter
    return line
以下是等效的:

    for letter in board[row_index]:
        line = line + letter
    return line

另一个注释,因为您的函数返回一个给定的子列表的级联字符串版本,您可以考虑一行解决方案…

返回“”。加入(线路板[行索引])

…将字符串中的字符连接到
线路板[行索引]

join
被认为比
+=
更有效,因为它避免了子字符串的浪费构造和丢弃。(为每个
+=
创建一个完全不同的字符串)特别是对于长字符串,
join
是一个很好的解决方案。

问题是您总是返回列表的第一个元素。请记住,
return
会使函数立即完成。此外,可以删除变量
字母
。您不需要初始化它,因为它是for循环的一部分。最后,您的代码可以是:

def make_str_from_row(board, row_index):
    """ (list of list of str, int) -> str

    Return the characters from the row of the board with index row_index
    as a single string.

    >>> make_str_from_row([['H', 'O', 'U', 'S', 'E'], ['B', 'E', 'D'], ['C', 'H', 'E', 'E', 'S', 'E']], 2)
    'CHEESE'
    """

    return ''.join(board[row_index])

问题是您总是返回列表的第一个元素。请记住,
return
会使函数立即完成。此外,可以删除变量
字母
。您不需要初始化它,因为它是for循环的一部分。最后,您的代码可以是:

def make_str_from_row(board, row_index):
    """ (list of list of str, int) -> str

    Return the characters from the row of the board with index row_index
    as a single string.

    >>> make_str_from_row([['H', 'O', 'U', 'S', 'E'], ['B', 'E', 'D'], ['C', 'H', 'E', 'E', 'S', 'E']], 2)
    'CHEESE'
    """

    return ''.join(board[row_index])

摆脱你的外环。对板中的字母[行索引]执行
处理内部循环。或者去掉所有循环,只返回“.join”(board[row\u index])
。谢谢@StevenRumbalski非常清晰、简洁的解释!摆脱你的外环。对板中的字母[行索引]执行
处理内部循环。或者去掉所有循环,只返回“.join”(board[row\u index])
。谢谢@StevenRumbalski非常清晰、简洁的解释!谢谢@synchronizer!有一件事我有点困惑。为什么函数返回子列表[0]。如果函数迭代每个子列表,为什么不返回所有子列表?谢谢@不客气。为了回答您的问题,函数ret仅以字符串形式运行子列表0,因为您在循环中提前返回。也就是说,在循环有机会从
子列表的其余部分添加字符之前,您将在第一次迭代结束时返回。(但这是不受欢迎的行为)。那有用吗?太棒了!头痛消除!:DThanks@synchronizer!有一件事我有点困惑。为什么函数返回子列表[0]。如果函数迭代每个子列表,为什么不返回所有子列表?谢谢@不客气。为了回答您的问题,函数ret仅以字符串形式运行子列表0,因为您在循环中提前返回。也就是说,在循环有机会从
子列表的其余部分添加字符之前,您将在第一次迭代结束时返回。(但这是不受欢迎的行为)。那有用吗?太棒了!头痛消除!:D