Suppose that I have the following list of lists containing lists:
samples = [
# First sample
[
# Think 'x' as in input variable in ML
[
['A','E'], # Data
['B','F'] # Metadata
],
# Think 'y' as in target variable in ML
[
['C','G'], # Data
['D','H'], # Metadata
]
],
# Second sample
[
[
['1'],
['2']
],
[
['3'],
['4']
]
]
]
The output that I'm after looks like the following:
>>> samples
[
['A','E','1'], # x.data
['B','F','2'], # x.metadata
['C','G','3'], # y.data
['D','H','4'] # y.metadata
]
My question is that does there exist a way to utilize Python's zip
function and maybe some list comprehensions to achieve this?
I have searched for some solutions, but for example this and this deal with using zip
to address different lists, not inner lists.
A way to achieve this could very well be just a simple iteration over the samples like this:
x,x_len,y,y_len=[],[],[],[]
for sample in samples:
x.append(sample[0][0])
x_len.append(sample[0][1])
y.append(sample[1][0])
y_len.append(sample[1][1])
samples = [
x,
x_len,
y,
y_len
]
I'm still curious if there exists a way to utilize zip
over for
looping the samples and their nested lists.
Note that the data
and metadata
can vary in length across samples.