Как объединить содержимое вложенного списка без потери подсписков?

У меня есть следующий вложенный список строк:

my_list = [['Yesterday I was talking to a friend who is considering applying for a job here at Tailwind.', 'He asked me why I took the job of Director of Marketing and Growth back in July, and among the many things I told him was this….'], ['Nearly every day somebody from Tailwind’s product development team talks at considerable length with one of our members, all of these conversations get written up, shared with the whole team on Slack (our instant messaging system), and they help us decide what features to build.', 'Around the office we call these “cust dev calls”, short for customer development calls, and they’re constant.'], ['Why fight over what you think customers want?'], ['My friend was impressed.', 'At his last job decisions about what features to build were considered too important to be made by anybody but the CEO, based on his considerable knowledge of the market.', 'In fact that was one of my friend’s biggest frustrations working there – that his ideas were never taken very seriously because his CEO always knew better than he did.',  'Or he thought he knew better.',  'The product my friend built there hasn’t found success yet.'], ['Of course there are a group of people who know what your customers want even better than you or your well-informed CEO do, and those people are your customers.','So why not cut out the middleman, ask them what their biggest frustrations are, see what ideas they have to solve them; think carefully about the best product you could build to overcome those frustrations, and then build what it is your customer really want.']]

Я хотел бы разделить на один элемент список подсписков my_list, что-то вроде (*):

[['Yesterday I was talking to a friend who is considering applying for a job here at Tailwind. He asked me why I took the job of Director of Marketing and Growth back in July, and among the many things I told him was this….'], ['Nearly every day somebody from Tailwind’s product development team talks at considerable length with one of our members, all of these conversations get written up, shared with the whole team on Slack (our instant messaging system), and they help us decide what features to build. Around the office we call these “cust dev calls”, short for customer development calls, and they’re constant.'], ['Why fight over what you think customers want?'], ['My friend was impressed. At his last job decisions about what features to build were considered too important to be made by anybody but the CEO, based on his considerable knowledge of the market. In fact that was one of my friend’s biggest frustrations working there – that his ideas were never taken very seriously because his CEO always knew better than he did. Or he thought he knew better. The product my friend built there hasn’t found success yet.'], ['Of course there are a group of people who know what your customers want even better than you or your well-informed CEO do, and those people are your customers. So why not cut out the middleman, ask them what their biggest frustrations are, see what ideas they have to solve them; think carefully about the best product you could build to overcome those frustrations, and then build what it is your customer really want.']]

Я знаю, что для объединения всех элементов каждого подсписка my_list ты можешь сделать:

my_list.join('')

Поэтому я попытался:

' '.join(itertools.chain(*my_list))

Однако я потерял свои списки. Любая идея о том, какой самый быстрый способ получить (*)

2 ответа

Решение

Просто создайте 1 строку из подсписков, используя join, но храните его в одном предмете list:

[[' '.join(x)] for x in my_list]

задавать my_list = [["a", "b"], ["c", "d"],["e","f"]]результат

[['a b'], ['c d'], ['e f']]

однако не очень полезно создавать подсписки с одним элементом (если вы не хотите добавлять элементы позже). Вы можете упростить это с помощью:

[' '.join(x) for x in my_list]

вы получите:

['a b', 'c d', 'e f']
>>> my_list = [["one", "two"], ["three", "four"]]
>>> [[' '.join(x)] for x in my_list]
[['one two'], ['three four']]
>>> map(lambda x: [' '.join(x)], my_list)
[['one two'], ['three four']]

Два подхода

Другие вопросы по тегам