Suppose I have two lists given by,
list1 = [[2,3,1,3],[2,4,6,2]]
list2 = [[1,2,3,4],[4,3,2,1]]
How would one convert to a 3d list?
So that to access list1, one would:
list3[0] = list1
list3[1] = list2
where print list3 would provide:
[[[2,3,1,3],[2,4,6,2]],[[1,2,3,4],[4,3,2,1]]]
I have tried adding, but that doesn't seem to work.
list1 = [[2,3,1,3],[2,4,6,2]]
list2 = [[1,2,3,4],[4,3,2,1]]
list3 = [list1, list2]
You actually need to deepcopy the tuples/lists to avoid creating references:
list1 = [2,3,1,3],[2,4,6,2]
list2 = [1,2,3,4],[4,3,2,1]
from copy import deepcopy
l3 = list(map(list, map(deepcopy,(list1, list2))))
print(l3[0])
print(l3[1])
[[2, 3, 1, 3], [2, 4, 6, 2]]
[[1, 2, 3, 4], [4, 3, 2, 1]]
If you have lists not actually tuples you can remove the outer map:
l3 = list(map(deepcopy,(list1, list2)))
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.