[英]how to remove the square bracket that are leftover in list with python?
我已經將結果生成到這樣的列表中
In[11]: a
Out[11]: [[[1, 3, 2, 4], [1, 3, 2, 6]],
[[2, 4], [2, 6]],
[[3, 2, 4], [3, 2, 6]],
[[4]],
[[5, 4]],
[[6]]]
但我想刪除由此產生的剩余方括號
[[1, 3, 2, 4], [1, 3, 2, 6],[2, 4], [2, 6],[3, 2, 4], [3, 2, 6],[4],[5, 4],[6]]
我試過sum(a,[])
來減少一維,但結果如下所示
[1, 3, 2, 4, 1, 3, 2, 6, 2, 4, 2, 6, 3, 2, 4, 3, 2, 6, 4, 5, 4, 6]
並嘗試 np.squeeze(a) 但結果如下所示
array([list([1, 3, 2, 4]), list([1, 3, 2, 6]), list([2, 4]), list([2, 6]),
list([3, 2, 4]), list([3, 2, 6]), list([4]), list([5, 4]),
list([6])], dtype=object)
對匯總此類列表有什么建議嗎?
先感謝您
你可以試試這個:
a = [[[1, 3, 2, 4], [1, 3, 2, 6]],
[[2, 4], [2, 6]],
[[3, 2, 4], [3, 2, 6]],
[[4]],
[[5, 4]],
[[6]]]
output = [elem for output_list in a for elem in output_list]
print(output)
嘗試這個。
a = [[[1, 3, 2, 4], [1, 3, 2, 6]],
[[2, 4], [2, 6]],
[[3, 2, 4], [3, 2, 6]],
[[4]],
[[5, 4]],
[[6]]]
fl = []
for i in a:
for j in i:
fl.append(j)
print fl
str(a).replace('[[','[').replace(']]',']')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.