返回列表元素的列表 [英] Returning a list of list elements

查看:584
本文介绍了返回列表元素的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助写这将需要一个单独的列表,并返回一个不同的列表,列表中的每一个元素是在自己原有的列表功能。

我知道,我会通过我通过再追加取决于值是否已在我的列表中选择值或创建一个子列表和子列表添加到最终名单原始列表进行迭代。

一个例子是:

 输入:[1,2,2,2,3,1,1,3]输出:[[1,1,1],[2,2,2],[3,3]


解决方案

我做这两个步骤:

 >>>进口藏品
>>>输入= [1,2,2,2,3,1,1,3]
>>>数= collections.Counter(输入)
>>>计数
计数器({1:3,2:3,3:2})
>>>输出= [[关键] *计数键,counts.items数()]
>>>输出
[[1,1,1],[2,2,2],[3,3]

(这些恰巧在排序编号顺序,以及在首次亮相的顺序其实只是巧合这里。计数器,像正常的字典,存储钥匙以任意顺序,你应该假设 [3,3],[1,1,1],[2,2,2] 将是一种可能的结果。如果这是不能接受的,你需要更多的工作。)


那么,它是如何工作的?

第一步是创建一个 计数 ,这是字典取得用于计算每个键的出现。一个关于它的许多漂亮的事情之一是,你可以通过它可迭代的(如列表),它会向上计数的每个元素出现多少次。这是一个微不足道的一行代码,这是显而易见的,可读的,一旦你知道如何计数的作品,它甚至对有效率的东西也可能会被*

但是,这是不是你想要的输出格式。我们如何获得呢?好了,我们必须得到从 1回:3 (意为 1 3份),以 [1,1,1] )。你可以写为 [重点] *计数 **,其余的只是一个沼泽标准列表COM prehension。


如果你看一下文档的收藏模块,他们一个链接到的来源。在STDLIB很多模块都是这样的,因为他们是想作为源$ C ​​$ C从和可用code学习。所以,你应该能够找出如何在计数构造的作品。 (它基本上只是调用了 _count_elements 函数)。因为这是计数器的唯一部分你实际使用超出了基本字典,你可以只写你自己的那部分。 (但实际上,一旦你了解它是如何工作的,有没有好的理由不使用它,对吗?)


<子> *对于每一个元素,它只是在做一个哈希表查找(并在需要时插入)和 + = 1 。而在CPython的,这一切都发生在合理,优化的C

<子> **请注意,我们不必担心是否使用 [重点] *计数 [重点_范围内(数)] 在这里,因为值必须是不可变的,或者至少是一个平等的身份一样好的类型,否则他们就不能用作键。

I need help writing a function that will take a single list and return a different list where every element in the list is in its own original list.

I know that I'll have to iterate through the original list that I pass through and then append the value depending on whether or not the value is already in my list or create a sublist and add that sublist to the final list.

an example would be:

input:[1, 2, 2, 2, 3, 1, 1, 3]

Output:[[1,1,1], [2,2,2], [3,3]]

解决方案

I'd do this in two steps:

>>> import collections
>>> inputs = [1, 2, 2, 2, 3, 1, 1, 3]
>>> counts = collections.Counter(inputs)
>>> counts
Counter({1: 3, 2: 3, 3: 2})
>>> outputs = [[key] * count for key, count in counts.items()]
>>> outputs
[[1, 1, 1], [2, 2, 2], [3, 3]]

(The fact that these happen to be in sorted numerical order, and also in the order of first appearance, is just a coincidence here. Counters, like normal dictionaries, store their keys in arbitrary order, and you should assume that [[3, 3], [1, 1, 1], [2, 2, 2]] would be just as possible a result. If that's not acceptable, you need a bit more work.)


So, how does it work?

The first step creates a Counter, which is just a special subclass of dict made for counting occurrences of each key. One of the many nifty things about it is that you can just pass it any iterable (like a list) and it will count up how many times each element appears. It's a trivial one-liner, it's obvious and readable once you know how Counter works, and it's even about as efficient as anything could possibly be.*

But that isn't the output format you wanted. How do we get that? Well, we have to get back from 1: 3 (meaning "3 copies of 1") to [1, 1, 1]). You can write that as [key] * count.** And the rest is just a bog-standard list comprehension.


If you look at the docs for the collections module, they start with a link to the source. Many modules in the stdlib are like this, because they're meant to serve as source code for learning from as well as usable code. So, you should be able to figure out how the Counter constructor works. (It's basically just calling that _count_elements function.) Since that's the only part of Counter you're actually using beyond a basic dict, you could just write that part yourself. (But really, once you've understood how it works, there's no good reason not to use it, right?)


* For each element, it's just doing a hash table lookup (and insert if needed) and a += 1. And in CPython, it all happens in reasonably-optimized C.

** Note that we don't have to worry about whether to use [key] * count vs. [key for _ in range(count)] here, because the values have to be immutable, or at least of an "equality is as good as identity" type, or they wouldn't be usable as keys.

这篇关于返回列表元素的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆