1

I have a NumPy dataset with sentence IDs and descriptions values. I would like to create an ordered list in my desired format which is... [['value'], ['value']] containing just description values. The order of the new list must stay the same as the original so I can match them back to the IDs later.

My issue is that I can't achieve the desired format for the new list instead of [['value'], ['value']] I get ['value', 'value'], why?

This is what I have tried:

Original input data:

[
[UUID('11ea1bca-eb95-4dc8-8cb9-c7d70a806679') 'the quick brown fox']
[UUID('339619ab-bd17-401f-82c8-a927145d52cf')' jumps over the lazy dog']
]

Created a new array to store just the description values:

description = np.array([description[1] for description in chunk])

However, this gives me the following output:

['the quick brown fox','jumps over the lazy dog']

My desired output is:

[['the quick brown fox'],[jumps over the lazy dog']]

How can I achieve my desired output keeping the original order?

  • Using Python 3.8
  • The original list is from np.array_split(book_information, chunk_size) which I loop over in chunks
0

1 Answer 1

2

You can simply add the additional dimension when defining your array: description = np.array([[description[1]] for description in chunk]).

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

2 Comments

Seem like I was not far off, Thank you! Will the order in which they are added stay the same? i.e I can be assured that the first ['the quick brown fox'] in the new list will be the same as the first in the original?
Yes, the order won't be changed: this comprehension is equivalent to a for loop that would go over the elements of the chunk in order, and appending them to the end of description as it goes.

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.