Skip to content

Instantly share code, notes, and snippets.

@HoweChen
Created March 14, 2019 08:52
Show Gist options
  • Save HoweChen/11923e0b1257149bccf54f1036955766 to your computer and use it in GitHub Desktop.
Save HoweChen/11923e0b1257149bccf54f1036955766 to your computer and use it in GitHub Desktop.
[一个for循环实现双向遍历]一个for循环实现双向遍历 #Python
from itertools import chain # 这里要用itertool里的chain
# 因为在python2里 range() 返回list
# python3 里range 返回iterator,不能直接相加,要用chain方法
for i in chain(range(n), range(n)[::-1]): # 这个写法可以实现一个for里双向遍历
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment