我想使用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