1.10小节完成
This commit is contained in:
38
cookbook/c01/p10_remove_duplicate.py
Normal file
38
cookbook/c01/p10_remove_duplicate.py
Normal file
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- encoding: utf-8 -*-
|
||||
"""
|
||||
Topic: 消除序列重复值并保持顺序
|
||||
Desc :
|
||||
"""
|
||||
|
||||
|
||||
def dedupe(items):
|
||||
"""元素都是hashable"""
|
||||
seen = set()
|
||||
for item in items:
|
||||
if item not in seen:
|
||||
yield item
|
||||
seen.add(item)
|
||||
|
||||
|
||||
def dedupe2(items, key=None):
|
||||
"""元素不是hashable的时候"""
|
||||
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)
|
||||
|
||||
|
||||
def remove_dup():
|
||||
a = [1, 5, 2, 1, 9, 1, 5, 10]
|
||||
print(list(dedupe(a)))
|
||||
|
||||
a = [{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 1, 'y': 2}, {'x': 2, 'y': 4}]
|
||||
print(list(dedupe2(a, key=lambda d: (d['x'], d['y']))))
|
||||
print(list(dedupe2(a, key=lambda d: d['x'])))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
remove_dup()
|
||||
@@ -38,7 +38,7 @@
|
||||
a.items() & b.items() # { ('y', 2) }
|
||||
|
||||
这些操作也可以用于修改或者过滤字典元素。
|
||||
比如,假如你想以现有字典构造一个排除几个执行键的新字典。
|
||||
比如,假如你想以现有字典构造一个排除几个指定键的新字典。
|
||||
下面利用字典推导来实现这样的需求:
|
||||
|
||||
.. code-block:: python
|
||||
@@ -51,7 +51,7 @@
|
||||
----------
|
||||
讨论
|
||||
----------
|
||||
一个字典就是一个键的集合对值的集合的映射关系。
|
||||
一个字典就是一个键集合与值集合的映射关系。
|
||||
字典的keys()方法返回一个展现键集合的键视图对象。
|
||||
键视图的一个很少被了解的特性就是它们也支持集合操作,比如集合并、交、差运算。
|
||||
所以,如果你想对集合的键执行一些普通的集合操作,可以直接使用键视图对象而不用先将它们转换成一个set。
|
||||
|
||||
@@ -1,18 +1,82 @@
|
||||
================================
|
||||
=============================
|
||||
1.10 删除序列相同元素并保持顺序
|
||||
================================
|
||||
=============================
|
||||
|
||||
----------
|
||||
问题
|
||||
----------
|
||||
todo...
|
||||
怎样在一个序列上面保持元素顺序的同时消除重复的值?
|
||||
|
||||
----------
|
||||
解决方案
|
||||
----------
|
||||
todo...
|
||||
如果序列上的值都是hashable类型,那么可以很简单的利用集合或者生成器来解决这个问题。比如:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
def dedupe(items):
|
||||
seen = set()
|
||||
for item in items:
|
||||
if item not in seen:
|
||||
yield item
|
||||
seen.add(item)
|
||||
下面是使用上述函数的例子:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> a = [1, 5, 2, 1, 9, 1, 5, 10]
|
||||
>>> list(dedupe(a))
|
||||
[1, 5, 2, 9, 10]
|
||||
>>>
|
||||
这个方法仅仅在序列中元素为hashable的时候才管用。
|
||||
如果你想消除元素不可哈希(比如dict类型)的序列中重复元素的话,你需要将上述代码稍微改变一下,就像这样:
|
||||
|
||||
.. 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)
|
||||
|
||||
这里的key参数指定了一个函数,将序列元素转换成hashable类型。下面是它的用法示例:
|
||||
|
||||
.. 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}]
|
||||
>>>
|
||||
|
||||
如果你想基于单个字段、属性或者某个更大的数据结构来消除重复元素,第二种方案同样可以胜任。
|
||||
|
||||
----------
|
||||
讨论
|
||||
----------
|
||||
todo...
|
||||
如果你仅仅就是想消除重复元素,通常可以简单的构造一个集合。比如:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> a
|
||||
[1, 5, 2, 1, 9, 1, 5, 10]
|
||||
>>> set(a)
|
||||
{1, 2, 10, 5, 9}
|
||||
>>>
|
||||
|
||||
然而,这种方法不能维护元素的顺序,生成的结果中的元素位置被打乱。而上面的方法可以避免这种情况。
|
||||
|
||||
在本节中我们使用了生成器函数让我们的函数更加通用,不仅仅是局限于列表处理。
|
||||
比如,如果如果你想读取一个文件,消除重复行,你可以很容易像这样做:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
with open(somefile,'r') as f:
|
||||
for line in dedupe(f):
|
||||
...
|
||||
|
||||
上述关键字函数参数模仿了sorted(),min()和max()等内置函数的相似功能。
|
||||
可以参考1.8和1.13小节了解更多。
|
||||
|
||||
Reference in New Issue
Block a user