如何加入嵌套列表的内容而不丢失子列表?

时间:2016-12-02 20:10:18

标签: python string python-3.x itertools

我有以下嵌套的字符串列表:

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 个答案:

答案 0 :(得分:1)

>>> 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']]

两种方法

答案 1 :(得分:1)

只需使用join从子列表创建1个字符串,但将其存储在单个项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']