Python 列表去重怎么用暴力枚举法写出 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 2018-08-30 00:19:06 +08:00 3986 次点击
    这是一个创建于 2653 天前的主题,其中的信息可能已经有所发展或是发生改变。
    希望各位大佬能提供代码,需求是时间复杂度最小
    19 条回复    2018-09-03 17:55:00 +08:00
    swulling
        1
    swulling  
       2018-08-30 00:24:44 +08:00 via iPhone   2
    set(x)
    OpenJerry
        2
    OpenJerry  
       2018-08-30 00:26:22 +08:00 via Android
    一楼正解,此贴终结
    ITisCool
        3
    ITisCool  
       2018-08-30 00:28:53 +08:00 via iPhone
    list(set(x))
    kumfo
        4
    kumfo  
       2018-08-30 00:29:12 +08:00
    赞同二楼说的一楼正解,此贴终结。
    Alerta
        5
    Alerta  
       2018-08-30 00:48:44 +08:00 via iPhone
    赞同三楼说的赞同二楼说的一楼正解,此贴终结。
    conn4575
        6
    conn4575  
       2018-08-30 06:59:19 +08:00 via Android
    如果要求保持顺序,list({i:None for i in x}.keys())
    conn4575
        7
    conn4575  
       2018-08-30 06:59:43 +08:00 via Android
    忘记说了,python3
    0xABCD
        8
    0xABCD  
       2018-08-30 08:15:37 +08:00 via Android
    @conn4575 python3.5 也是无序的,3.6 是有序的,但是不要依赖这一实现
    ericls
        9
    ericls  
       2018-08-30 08:47:29 +08:00 via iPhone
    @conn4575 3.6 以上 保证顺序
    ericls
        10
    ericls  
       2018-08-30 08:47:44 +08:00 via iPhone
    @0xABCD 3.6 以上的 cpython 保证顺序
    so1n
        11
    so1n  
       2018-08-30 10:06:57 +08:00
    @ericls 请问你们是指 3.6 的 set()有序还算 list(set())有序?
    exhades
        12
    exhades  
       2018-08-30 10:08:31 +08:00 via Android
    set 就够了
    xinhangliu
        13
    xinhangliu  
       2018-08-30 10:53:39 +08:00 via Android
    @so1n 指的是 Dict
    reself
        14
    reself  
       2018-08-30 13:00:47 +08:00 via Android
    @conn4575 这里不能保持有序吧,生成 dict 时失序了,再调用 keys 仍然无序
    so1n
        15
    so1n  
       2018-08-30 13:02:55 +08:00
    @xinhangliu 3.6 的 dict 变成有序的吗
    ghhardy
        16
    ghhardy  
       2018-08-30 13:45:07 +08:00   1
    楼主是否看过 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
        17
    huangke  
       2018-09-01 16:07:34 +08:00
    res = []
    lists = = [res.append(i) for i in target if i not in res]
    wersonliu9527
        18
    wersonliu9527  
       2018-09-03 09:59:50 +08:00
    我忘了在哪看过的一个以前没见过的操作,去重保留顺序
    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
        19
    thautwarm  
       2018-09-03 17:55:00 +08:00
    python sorteddict 的实现已经默认了,原因是这个涉及到 dataclass 和 NamedTuple 的实现。
    可以依赖 sorted dict 了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5207 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 07:45 PVG 15:45 LAX 23:45 JFK 02:45
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86