2

I have a 3D list ll which can be of size 100 K * 10 * 3

ll = [
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10,11,12]], [[6, 7, 8],[12, 13, 14]], [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
]

I want it to be

ll = [[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10,11,12]], [[6, 7, 8],[12, 13, 14], [0, 0, 0], [0, 0, 0]], [[10, 20, 30], [40, 50, 60], [70, 80, 90], [0,0,0]]]

so that I can create a1 = np.array(l1)

a1

array([
[[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
[[6,7,8], [12,13,14], [0,0,0], [0,0,0]]
[[10, 20, 30], [40, 50, 60], [70, 80, 90], [0,0,0]]
])

I have read the following but they are for 2D, i am not able to do it for 3D.

https://stackoverflow.com/a/38619333/5202279

https://stackoverflow.com/a/43149308/5202279

6
  • I don't understand your question. Could you be more precise? Why don't you just add the missing zeros? Commented Dec 18, 2019 at 12:33
  • Are the innermost lists also of variable length? Or do they always contain three elements? Commented Dec 18, 2019 at 12:40
  • they are coordinates so will always have 3 elements. Commented Dec 18, 2019 at 12:41
  • ll[len(ll) - 1].append([0, 0, 0]) Do you mean something like this? Commented Dec 18, 2019 at 12:41
  • @IcesHay I think the idea is to have something more general working, i.e. when you don't know how many zeros need to be padded and where. Commented Dec 18, 2019 at 12:42

2 Answers 2

3

Here's a way that allocates the NumPy array up front then copies the data over. Assuming you don't actually need the expanded ll, this should use less memory than appending the 0-triples to ll before creating a1:

a1 = np.zeros((len(ll), max([len(k) for k in ll]), 3))
for ctr,k in enumerate(ll):
     a1[ctr,:len(k),:] = k

a1
array([[[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.],
        [10., 11., 12.]],

       [[ 6.,  7.,  8.],
        [12., 13., 14.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]],

       [[10., 20., 30.],
        [40., 50., 60.],
        [70., 80., 90.],
        [ 0.,  0.,  0.]]])

max([len(k) for k in ll]) tells us the maximum number of triples in any member of ll. We allocate a 0-initialized NumPy array of the desired size. Then in the loop, smart indexing tells us where in a1 to copy each member of ll.

Sign up to request clarification or add additional context in comments.

Comments

1

Iterate over all elements in the list, which are also lists and get the max length. Then append zeros to every "sublist" that does not have the max length.

m = max([len(k) for k in ll])

for i in range(0, len(ll)):
    while len(ll[i]) < m:
        ll[i].append([0, 0, 0])

2 Comments

Good method, but max is the name of a function in Python already, so probably best not to reuse it for a variable name. You can do something like m = max([len(k) for k in ll]) to turn the first part of your answer into something more Pythonic
You are right. I will update my answer. Thanks for your feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.