Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove_duplicate_elements,从序列中删除重复的项并且保持元素间的顺序不变 #77

Closed
mindjun opened this issue Jul 2, 2018 · 0 comments
Labels
Milestone

Comments

@mindjun
Copy link
Contributor

mindjun commented Jul 2, 2018

背景

#63 中只能删除列表中的重复元素,并且因为使用set,导致去重后的列表中的元素是无序的

步骤

  • 添加一个key用以指定一个函数将序列中的元素转换为可哈希类型,这样就可以检测重复元素了
  • 新建一个set类型,如果发现不是重复的元素,就将它加入到set中,以保持有序

举例

>>> list1 = [1,23,4,5,6,1,7,23]
>>> list(remove_duplicate_elements(list1))
[1, 23, 4, 5, 6, 7]
>>> list2 = [{'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}]
>>> list(remove_duplicate_elements(a, key=lambda d:(d['x'],d['y'])))
[{'x': 1, 'y': 2}, {'x': 1, 'y': 3}]
@mindjun mindjun added this to the v1.0.15 milestone Jul 2, 2018
@mindjun mindjun added the feature label Jul 2, 2018
mindjun pushed a commit to mindjun/fishbase that referenced this issue Jul 7, 2018
@mindjun mindjun mentioned this issue Jul 7, 2018
@itaa itaa closed this as completed Jul 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants