原始數據集
raw=['[1,2]','[3,4]','[4,5]']
# 合并結果為一個大列表
res_li=[1,2,3,4,5]
方法1:通過itertools
import itertools
raw=['[1,2]','[3,4]','[4,5]']
list(itertools.chain(*map(eval, raw)))
li=[[1,2],[3,4],[4,5]]
list(itertools.chain(*li))
方法2:通過Append
temp_li = []
for res in raw:
for i in eval(res):
temp_li.append(i)
方法3:通過循環
res=[x for j in raw for x in eval(j)]
import json
res=[x for j in raw for x in json.loads(j)]
方法4:通過extend
temp_li = []
for res in raw:
temp_li.extend(eval(res))