Five ways to expand nested lists in Python

1. Presentation of the question

Someone in the WeChat group asked how to convert the following content into a list:

After conversion:

"[["007674","ICBC Industrial Upgrade Stock A","GYCYSJGPA","1.3574"],["007675","ICBC Industrial Upgrade Stock C","GYCYSJGPC","1.3205"],["001719","ICBC National Strategic Stock","GYGJZLGP","2.25"]]"

After conversion:

["007674","ICBC Industrial Upgrade Stock A","GYCYSJGPA","1.3574","007675","ICBC Industrial Upgrade Stock C","GYCYSJGPC","1.3205","001719","ICBC National Strategic Stock","GYGJZLGP","2.25"]

2. Problem analysis

To achieve the above conversion, the first step is to convert the string into a list, we can use the eval() function. Then I have to traverse the nested list, and unpack a sublist, either through list comprehension, or through asterisks or list addition, or collection merging and other methods.

The above nested list is a standard list. If it is not a standard list, we need to use isinstance() to judge whether it is a list. If so, we will expand the operation. A recursive method may be used here.

1. List comprehension method

First of all, this is a nested list. If there are double quotes outside, it is a string, which can be removed by eval(), and then solved by list comprehension.

data = '[["007674","工银产业升级股票A","GYCYSJGPA","1.3574"],["007675","工银产业升级股票C","GYCYSJGPC","1.3205"],["001719","工银国家战略股票","GYGJZLGP","2.25"]]'
s=[]
print([item for ls in eval(data) for item in ls])

Code running display:

2. for loop method

data = '[["007674","工银产业升级股票A","GYCYSJGPA","1.3574"],["007675","工银产业升级股票C","GYCYSJGPC","1.3205"],["001719","工银国家战略股票","GYGJZLGP","2.25"]]'
s=[]
for lst in eval(data):
   for ls in lst:
       s.append(ls)
print(s)

Code running display:

3. You can use asterisk to unpack and output

Use an asterisk to unpack the list, the code is as follows. But in the end, it is only output, and the list is not generated, which does not meet the requirements of the title.

data = '[["007674","工银产业升级股票A","GYCYSJGPA","1.3574"],["007675","工银产业升级股票C","GYCYSJGPC","1.3205"],["001719","工银国家战略股票","GYGJZLGP","2.25"]]'
s=[]
for lst in eval(data):
    print([*lst],sep=",",end="") 第一种方法

Code running display:

4. List addition

Simply put, after the string is converted into a list, the elements in it are added through a loop.

lst = '[["007674","工银产业升级股票A","GYCYSJGPA","1.3574"],["007675","工银产业升级股票C","GYCYSJGPC","1.3205"],["001719","工银国家战略股票","GYGJZLGP","2.25"]]'
s=[]
for i in eval(lst):
    s+=i
print(s)

5. Set union

Convert the list into a set, use the method of union to find the combined set, and finally convert the list into a list.

lst = '[["007674","工银产业升级股票A","GYCYSJGPA","1.3574"],["007675","工银产业升级股票C","GYCYSJGPC","1.3205"],["001719","工银国家战略股票","GYGJZLGP","2.25"]]'
s=set()
for i in lst:
    s= s|set(i)
print(list(s))

The effect of the code after running

3. Matters needing attention

  1. The three methods lead to the same goal, please use them selectively. Personally, I prefer the list expression method, which is simpler and more efficient.
  2. In Python we can unpack a list with an asterisk. But asterisk unpacking doesn't work with list comprehensions, so it's a bit of a shame.

Guess you like

Origin blog.csdn.net/henanlion/article/details/131099857