Python面试真题-how do I iterate over a sequence in reverse order?

【Python面试真题】-how do I iterate over a sequence in reverse order?

for x in reversed(sequence):
    ... # do something with x..

如果不是list, 最通用但是稍慢的解决方案是:

for i in range(len(sequence)-1, -1, -1):
    x = sequence[i]
    <do something with x>

相关新闻

历经多年发展,已成为国内好评如潮的Linux云计算运维、SRE、Devops、网络安全、云原生、Go、Python开发专业人才培训机构!