此代码失败:
fCamel = 'F'
bCamel = 'B'
gap = ' '
k = ['F', ' ', 'B', 'F']
def solution(formation):
return ((formation.index(bCamel) > (len(formation) - 1 - (formation.reverse()).index(fCamel))))
solution(k)
我得到一个异常,说 AttributeError: 'NoneType' object has no attribute 'index'
。
我知道问题在于list.reverse()
返回None
,就地修改列表。我想在反向列表上使用 .index
。有没有办法可以避免在索引列表之前使用单独的语句来反转列表?怎么样?
最佳答案
您可以使用切片返回反向列表:
l[::-1]
关于python - 如何获取列表的反向副本(在 .reverse 之后链接方法时避免使用单独的语句)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4280691/