How to deal with deep nested Python list
来源:互联网 发布:商陆花软件是什么 编辑:程序博客网 时间:2024/06/03 16:26
As the most common and basic data struct in Python , List is used in many situations , but when we meet deep nested List ,how we deal with it?
- For fewer nested python list we can combine if clause and isinstance()BIF to handle it as follow eg(two level nested list):
lst=["level1.2",["level2.1","level2.2"],"level1.3"]for item in lst: if isinstance(item, list): for item1 in item: print (item1) else: print (item)
2.For deeply nested list , absolutely you can add more if clause and isinstance()BIF to handle, but there is a problem: many levels if judgements and too much code makes it hard to understand the program, so we try another way: define a function then do a selfcall(recursion) to access the list
flag = 0lst=["level1.2",["level2.1",["level2.21",["level2.211",["level2.211",["level2.211",["level2.211","level2.212"]]]]]],"level1.3"]def deep_list(lst): global flag for item in lst: if isinstance(item, list): flag=flag+1 print ("list level:",flag,"start") deep_list(item) else: print (item)deep_list(lst)
阅读全文
1 0
- How to deal with deep nested Python list
- How to deal with '/bin/rm:Argument list too long'
- How to deal with deadlock.
- How to deal with Gecko problem.
- How to deal with javadoc createProcess issue?
- How to deal with generation gap
- How to deal with International trade outsourcing?
- How to deal with difficult Boss
- How to deal with interlaced video
- [cernRoot]how to deal with TPavetext->SetTextAngle()?
- How to deal with telecom fraud?
- How to deal with an SVM with categorical attributes?
- how to deal with "undefined reference to XXX"
- How to deal with faculty,感觉自己的老师真是~~~~~~
- How to Deal with Messy Codes in Essbase
- How to Deal With Out Of Memory Errors
- Issue from FindBugs,and how to deal with them
- how to deal with error SPARK-5063 in spark
- mysql的replace into语句
- 笨办法学 Python · 续 练习 52:`moreweb`
- PAT 乙级 1002
- 生成器表达式和列表解析的区别
- Java中的Reference
- How to deal with deep nested Python list
- 开源项目——PullToRefreshListView的使用
- MAC 下定制Py交互式命令
- 判断单链表是否有环
- 求大神帮助
- ACM训练计划
- Oracle 数据库查询
- 2017/8/16训练日记(线段树简单入门)
- C++ 学习笔记