Home > front end >  List of lists of lists in Python
List of lists of lists in Python

Time:07-30

I am trying to convert a list Ii02 into a list of lists as shown in the expected output.

Ii02 = [[[0, 1], [0, 2], [1, 3], [4, 3], [2, 4]], [[0, 1], [0, 2], [1, 3], [4, 3], [3, 4], [2, 5]]]
for h in range(0,len(Ii02)):
    Ii03=[[[i] for i in Ii02[h]]]
    print(Ii03)

The current output is

[[[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[3, 4]], [[2, 5]]]]

The expected output is

[[[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[2, 4]]],
[[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[3, 4]], [[2, 5]]]]

CodePudding user response:

You can do that with a nested list comprehension:

[[[j] for j in i] for i in Ii02]

Output:

[[[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[2, 4]]], 
[[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[3, 4]], [[2, 5]]]]

CodePudding user response:

Here we go. The code is quite simple and self-explanatory.

Ii02 = [[[0, 1], [0, 2], [1, 3], [4, 3], [2, 4]], [[0, 1], [0, 2], [1, 3], [4, 3], [3, 4], [2, 5]]]
Ii03 = []
for h in range(0,len(Ii02)):
    Ii03.append([[i] for i in Ii02[h]])
print(Ii03)

Output

[[[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[2, 4]]], [[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[3, 4]], [[2, 5]]]]

CodePudding user response:

The code is simple.

Ii02 = [[[0, 1], [0, 2], [1, 3], [4, 3], [2, 4]], [[0, 1], [0, 2], [1, 3], [4, 3], [3, 4], [2, 5]]]
Ii03 = []

for h in Ii02:
    Ii03.append([[i] for i in h])
print(Ii03)

Output is:

[[[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[2, 4]]], [[[0, 1]], [[0, 2]], [[1, 3]], [[4, 3]], [[3, 4]], [[2, 5]]]]
  • Related