如何在不丢失子列表的情况下加入嵌套列表的内容?

Posted

技术标签:

【中文标题】如何在不丢失子列表的情况下加入嵌套列表的内容?【英文标题】:How to join the content of a nested list with out losing the sublists? 【发布时间】:2016-12-02 20:10:18 【问题描述】:

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

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))

但是我丢失了子列表。任何想法是获得 (*) 的最快方法

【问题讨论】:

您确定要一个单元素列表,而不是字符串列表吗?在我看来,问题不是“丢失子列表”;看起来问题是所有子列表中的所有字符串都连接成一个字符串,而不是单独的子列表单独连接。 感谢@user2357112 的帮助!是的,我实际上是想把所有东西都放在一个字符串中,用空格分隔的“元素” “我实际上是想把所有东西都放在一个字符串中”——这与你在问题中所说的相反。 可以通过不粘贴超长示例而仅粘贴较小的示例来改进问题,例如答案中使用的示例。 【参考方案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']

【讨论】:

好吧,我很困惑,实际上它很有用,因为我正在处理一些文件。谢谢! map 需要你做list(map(lambda x: [" ".join(x)],my_list)):在 python 3 中的一个 lambda 和一个列表转换,因为 map 现在返回一个迭代器:可读性较差。列表推导确实经过优化,所以它是最快的方法,除非一些专门的预编译包可以完成这项工作。【参考方案2】:
>>> 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']]

两种方法

【讨论】:

感谢您的帮助!所需的输出应该是:[['one two'], ['three four']] 已编辑——看起来怎么样? 使用生成器是最 Pythonesque 的,但你不会知道哪个是最快的,直到你计时。 小心:使用 python 3 你必须将你的 map 转换为 list 否则你会得到一个迭代器。这使得 mapfilter 在 python 3 中真的没那么有趣了。列表推导式就是这样。

以上是关于如何在不丢失子列表的情况下加入嵌套列表的内容?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不创建其他字段的情况下生成一个列表的嵌套 formik 表单或表单字段?

如何在不创建嵌套数组的情况下将多个变量添加到数组

如何在不重新索引的情况下将项目添加到 laravel 列表集合中?

如何在不停止递归的情况下返回递归函数中的值?

如何在不安装的情况下阅读共享的 Spotify 播放列表的内容?

如何在不更改原始列表的情况下更改新列表?