The following is the same idea, but with less code:
import torch
x = torch.tensor([[[0.7418, 0.3182, 0.4222, 0.0584, 0.0477]],
        [[0.4293, 0.3079, 0.2928, 0.8873, 0.9470]],
        [[0.6137, 0.3592, 0.2576, 0.8944, 0.4743]],
        [[0.6279, 0.2723, 0.2599, 0.6904, 0.9212]],
        [[0.2126, 0.2689, 0.2441, 0.8755, 0.4720]]])

z = torch.tensor([3, 3, 1, 4, 1])

# one line trick
(torch.arange(x.size(2)) < z[..., None]).unsqueeze(1) * x

This gives the desired result.