Skip to content

Instantly share code, notes, and snippets.

@tiye
Created May 1, 2012 04:10
Show Gist options
  • Save tiye/2564938 to your computer and use it in GitHub Desktop.
Save tiye/2564938 to your computer and use it in GitHub Desktop.
recursion of ESC
import types
cursor = '\t'
def recursion (arr):
copy = []
# 循环列表每个元素然后一个个附着到 copy 里面最后返回的
for item in arr:
if type (item) is types.ListType:
# 如果子列表存在 cursor 就把它提出放到列表同一级的后面一个位置, 否则原样递归
if cursor in item:
item.remove (cursor)
if len (item) > 0:
copy.append (item)
copy.append (cursor)
else:
copy.append (recursion (item))
else:
# 如果字符串中存在 cursor 就提出来放到字符串后边作为一个函数, 否则原样
if item.find (cursor) < 0:
copy.append (item)
else:
copy.append (item.replace (cursor, ''))
copy.append (cursor)
return copy
arr = ['3' , '5\t']
print (recursion (arr))
# output: ['3', '5', '\t']
arr = ['3', '555', ['4', '\t']]
print (recursion (arr))
# output: ['3', '555', ['4'], '\t']
arr = ['35', '65', ['4', ['\t', '45', ['5']]]]
print (recursion (arr))
# output: ['35', '65', ['4', ['45', ['5']], '\t']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment