【发布时间】:2017-02-27 05:51:14
【问题描述】:
我试图在 main 中运行我的 flatten(li) 函数,但是当我运行模块时它没有运行。但是当我在 shell 中输入“flatten(li)”时它正在工作。有任何想法吗?谢谢!
li = [0, 2, [[2, 3], 8, 100, None, [[None]]], -2]
def flatten(li):
i = 0
while i < len(li):
"only execute if the element is a list"
while isinstance(li[i], list):
"""taking the element at index i and sets it as the
i'th part of the list. so if l[i] contains a list
it is then unrolled or 'unlisted'"""
li[i:i + 1] = li[i]
i += 1
for element in li:
if not element and not isinstance(element, int):
li.remove(element)
return li
def main():
flatten(li)
if __name__ == '__main__':
main()
【问题讨论】:
-
你什么也看不到,因为你什么也没打印
标签: python shell python-idle