Home > Backend Development > Python Tutorial > Python-parsing of nested lists

Python-parsing of nested lists

高洛峰
Release: 2017-03-03 14:03:20
Original
1693 people have browsed it

A 3-level nested list m

m=["a",["b","c",["inner"]]]

needs parsing For the basic data items a, b, c, inner

Basic method of getting data items:

for i in m:

print i This can only take out the first level a, and combine it with a 2-level nested list ["b", "c", ["inner"]]

Combined with the built-in Functions and judgments can continue to parse this 2-level list

for i in m:
	if isinstance(i,list):
		for j in i:
			print j
	else: print i结果

a
b
c
['inner']
Copy after login

This 2-level nesting is also separated, but the list inside is not split, although it can continue The result is obtained after disassembly, but it is not the best choice

Constructor, iteratively parse this multi-layer nested list

 def printm(listin):
	for i in listin:
		if isinstance(i,list):
			printm(i)
		else: print i使用该函数直接解析嵌套列表,一次拆完

printm(m)
Copy after login

The result As follows:

a
b
c
inner
Copy after login

The above comprehensive analysis of Python-nested list list is all the content shared by the editor. I hope it can help It is a reference for everyone, and I hope everyone will support the PHP Chinese website.

For more articles related to Python-nested list parsing, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template