I have an array a
as follow : [[(0,0),(2,0)],[(1,1)], [(3,8)]]
So now I want to convert it like this: [(0,0),(2,0),(1,1), (3,8)]
How may I do that?
I had tried bellow code and successed, but I need some ideas better and faster.
nresult = []
for i in range(len(result)):
arr = result[i]
for j in range(len(arr)):
nresult.append(arr[j])
Can someone help me?
Thanks!
You can use reduce
from functools
like this
from functools import reduce
a = [[(0,0),(2,0)],[(1,1)], [(3,8)]]
res = reduce(lambda x,y: x+y,a)
print(res) # [(0, 0), (2, 0), (1, 1), (3, 8)]
If your nested-deep is certain, you can use chain from itertools package
from itertools import chain
data = [[(0,0),(2,0)],[(1,1)], [(3,8)]]
result = list(chain(*data))
You can use list comprehensions -
nested = [[(0,0),(2,0)],[(1,1)], [(3,8)]]
un_nested = [inside_element for element in nested for inside_element in element]
# Returns - [(0, 0), (2, 0), (1, 1), (3, 8)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.