我正在写一个函数,它需要返回列表中某个字符的最后一次出现的行和列。如果该字符不在列表中,则函数应该返回None。该函数忽略或跳过首次出现,然后将最后一次出现的行和列作为有序对返回。查找列表中某个字符的最后一次出现
Example:
lst = [['.','.','.','e'],
['A','A','.','e'],
['.','.','.','e'],
['.','X','X','X'],
['.','.','.','.'],
['.','y','Z','Z']]
#For this list of lists the function should return (5,3) for Z since it is in the 6th list,
#and is the 6th value (python starts the count at 0) and for X it should return (3,3)
我认为我当前的代码找到第一次出现的字符的行和列,但不是最后一次出现。我如何指示Python忽略第一次出现,而是返回最后一行的行和列?
代码:
def get_far_end(symbol,lot):
for i in range(len(lot)):
for j in lot[i]:
if j == symbol:
return i ,lot[i].index(j)
从此开始并退后一步,您发现的第一个事件将是您列表中的最后一个事件。 – Knells