推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
GJXxiaoBai
V2EX  ›  Python

Python 列表去重怎么用暴力枚举法写出

  •  
  •   GJXxiaoBai · Aug 30, 2018 · 4707 views
    This topic created in 3004 days ago, the information mentioned may be changed or developed.
    希望各位大佬能提供代码,需求是时间复杂度最小
    19 replies  •  2018-09-03 17:55:00 +08:00
    swulling
    swulling
    Aug 30, 2018 via iPhone❤️2
    1
    set(x)
    OpenJerry
    OpenJerry
    Aug 30, 2018 via Android
    2
    一楼正解,此贴终结
    ITisCool
    ITisCool
    Aug 30, 2018 via iPhone
    3
    list(set(x))
    kumfo
    kumfo
    Aug 30, 2018
    4
    赞同二楼说的一楼正解,此贴终结。
    Alerta
    Alerta
    Aug 30, 2018 via iPhone
    5
    赞同三楼说的赞同二楼说的一楼正解,此贴终结。
    conn4575
    conn4575
    Aug 30, 2018 via Android
    6
    如果要求保持顺序,list({i:None for i in x}.keys())
    conn4575
    conn4575
    Aug 30, 2018 via Android
    7
    忘记说了,python3
    0xABCD
    0xABCD
    Aug 30, 2018 via Android
    8
    @conn4575 python3.5 也是无序的,3.6 是有序的,但是不要依赖这一实现
    ericls
    ericls
    Aug 30, 2018 via iPhone
    9
    @conn4575 3.6 以上 保证顺序
    ericls
    ericls
    Aug 30, 2018 via iPhone
    10
    @0xABCD 3.6 以上的 cpython 保证顺序
    so1n
    so1n
    Aug 30, 2018
    11
    @ericls 请问你们是指 3.6 的 set()有序还算 list(set())有序?
    exhades
    exhades
    Aug 30, 2018 via Android
    12
    set 就够了
    xinhangliu
    xinhangliu
    Aug 30, 2018 via Android
    13
    @so1n 指的是 Dict
    reself
    reself
    Aug 30, 2018 via Android
    14
    @conn4575 这里不能保持有序吧,生成 dict 时失序了,再调用 keys 仍然无序
    so1n
    so1n
    Aug 30, 2018
    15
    @xinhangliu 3.6 的 dict 变成有序的吗
    ghhardy
    ghhardy
    Aug 30, 2018❤️1
    16
    楼主是否看过 python cookbook ?

    If the values in the sequence are hashable, the problem can be easily solved using a set and a generator. For example:

    def dedupe(items):
    seen = set()
    for item in items:
    if item not in seen:
    yield item seen.add(item)

    Here is an example of how to use your function:
    >>> a = [1, 5, 2, 1, 9, 1, 5, 10]
    >>> list(dedupe(a))
    [1, 5, 2, 9, 10]
    >>>

    This only works if the items in the sequence are hashable. If you are trying to eliminate duplicates in a sequence of unhashable types (such as dicts), you can make a slight change to this recipe, as follows:

    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)

    Here, the purpose of the key argument is to specify a function that converts sequence items into a hashable type for the purposes of duplicate detection. Here ’ s how it works:
    >>> 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}]
    >>>
    This latter solution also works nicely if you want to eliminate duplicates based on the value of a single field or attribute or a larger data structure.
    huangke
    huangke
    Sep 1, 2018
    17
    res = []
    lists = = [res.append(i) for i in target if i not in res]
    wersonliu9527
    wersonliu9527
    Sep 3, 2018
    18
    我忘了在哪看过的一个以前没见过的操作,去重保留顺序
    ccc = [1, 3, 5, 8, 9, 3, 5, 9, 10]
    res = sorted(set(ccc), key=ccc.index)
    print res
    =>[1, 3, 5, 8, 9, 10]
    thautwarm
    thautwarm
    Sep 3, 2018
    19
    python sorteddict 的实现已经默认了,原因是这个涉及到 dataclass 和 NamedTuple 的实现。
    可以依赖 sorted dict 了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1324 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 17:16 · PVG 01:16 · LAX 09:16 · JFK 12:16
    ♥ Do have faith in what you're doing.