如何将元素插入到程序指定级别的列表中?我的解决方案不是 Pythonic:
def listInsertDepth(l,e,i,lvl): # Insert element e into list l at depth lvl using list of indices i
if lvl < 0: # That is, if your depth level is invalid
return l
else:
assert len(i) == lvl+1 # One index for every level, plus for the actual insertion
s = l # A copy for tampering with
for index in range(lvl):
s = s[i[index]]
s.insert(i[-1],e)
return listInsertDepth(l,s,i[:-1],lvl-1)