我想使用for循环在列表列表中查找列表。从列表中返回特定列表,使用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)
摆脱你的外层循环。对于你的内部循环,请在board [row_index]:中输入':'。或者摆脱所有循环,只是'return''.join(board [row_index])'。 –
谢谢@StevenRumbalski非常清晰,简洁的说明! – sim