2014-09-02 13:57:50 +08:00
|
|
|
|
=============================
|
2014-09-02 04:46:28 +08:00
|
|
|
|
1.10 删除序列相同元素并保持顺序
|
2014-09-02 13:57:50 +08:00
|
|
|
|
=============================
|
2014-08-20 16:52:46 +08:00
|
|
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
|
问题
|
|
|
|
|
|
----------
|
2014-09-02 13:57:50 +08:00
|
|
|
|
怎样在一个序列上面保持元素顺序的同时消除重复的值?
|
2014-08-20 16:52:46 +08:00
|
|
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
|
解决方案
|
|
|
|
|
|
----------
|
2015-09-18 00:25:04 +08:00
|
|
|
|
如果序列上的值都是 ``hashable`` 类型,那么可以很简单的利用集合或者生成器来解决这个问题。比如:
|
2014-09-02 13:57:50 +08:00
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
def dedupe(items):
|
|
|
|
|
|
seen = set()
|
2015-11-14 18:03:55 +08:00
|
|
|
|
for item in items:
|
|
|
|
|
|
if item not in seen:
|
|
|
|
|
|
yield item
|
2017-07-10 20:02:24 +08:00
|
|
|
|
seen.add(item)
|
2014-09-02 13:57:50 +08:00
|
|
|
|
下面是使用上述函数的例子:
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
>>> a = [1, 5, 2, 1, 9, 1, 5, 10]
|
|
|
|
|
|
>>> list(dedupe(a))
|
|
|
|
|
|
[1, 5, 2, 9, 10]
|
|
|
|
|
|
>>>
|
2015-09-18 00:25:04 +08:00
|
|
|
|
这个方法仅仅在序列中元素为 ``hashable`` 的时候才管用。
|
2017-07-10 20:02:24 +08:00
|
|
|
|
如果你想消除元素不可哈希(比如 ``dict`` 类型)的序列中重复元素的话,你需要将上述代码稍微改变一下,就像这样:
|
2014-09-02 13:57:50 +08:00
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
def dedupe(items, key=None):
|
|
|
|
|
|
seen = set()
|
|
|
|
|
|
for item in items:
|
|
|
|
|
|
val = item if key is None else key(item)
|
|
|
|
|
|
if val not in seen:
|
|
|
|
|
|
yield item
|
|
|
|
|
|
seen.add(val)
|
|
|
|
|
|
|
2015-09-18 00:25:04 +08:00
|
|
|
|
这里的key参数指定了一个函数,将序列元素转换成 ``hashable`` 类型。下面是它的用法示例:
|
2014-09-02 13:57:50 +08:00
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
>>> a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]
|
|
|
|
|
|
>>> list(dedupe(a, key=lambda d: (d['x'],d['y'])))
|
|
|
|
|
|
[{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 2, 'y': 4}]
|
|
|
|
|
|
>>> list(dedupe(a, key=lambda d: d['x']))
|
|
|
|
|
|
[{'x': 1, 'y': 2}, {'x': 2, 'y': 4}]
|
|
|
|
|
|
>>>
|
|
|
|
|
|
|
|
|
|
|
|
如果你想基于单个字段、属性或者某个更大的数据结构来消除重复元素,第二种方案同样可以胜任。
|
2014-08-20 16:52:46 +08:00
|
|
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
|
讨论
|
|
|
|
|
|
----------
|
2014-09-02 13:57:50 +08:00
|
|
|
|
如果你仅仅就是想消除重复元素,通常可以简单的构造一个集合。比如:
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
>>> a
|
|
|
|
|
|
[1, 5, 2, 1, 9, 1, 5, 10]
|
|
|
|
|
|
>>> set(a)
|
|
|
|
|
|
{1, 2, 10, 5, 9}
|
|
|
|
|
|
>>>
|
|
|
|
|
|
|
|
|
|
|
|
然而,这种方法不能维护元素的顺序,生成的结果中的元素位置被打乱。而上面的方法可以避免这种情况。
|
|
|
|
|
|
|
|
|
|
|
|
在本节中我们使用了生成器函数让我们的函数更加通用,不仅仅是局限于列表处理。
|
2019-11-02 02:03:10 +08:00
|
|
|
|
比如,如果你想读取一个文件,消除重复行,你可以很容易像这样做:
|
2014-09-02 13:57:50 +08:00
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
with open(somefile,'r') as f:
|
|
|
|
|
|
for line in dedupe(f):
|
|
|
|
|
|
...
|
|
|
|
|
|
|
2015-09-18 00:25:04 +08:00
|
|
|
|
上述key函数参数模仿了 ``sorted()`` , ``min()`` 和 ``max()`` 等内置函数的相似功能。
|
2017-07-10 20:02:24 +08:00
|
|
|
|
可以参考 1.8 和 1.13 小节了解更多。
|