1
swulling 2018-08-30 00:24:44 +08:00 via iPhone 2
set(x)
|
2
OpenJerry 2018-08-30 00:26:22 +08:00 via Android
一楼正解,此贴终结
|
3
ITisCool 2018-08-30 00:28:53 +08:00 via iPhone
list(set(x))
|
4
kumfo 2018-08-30 00:29:12 +08:00
赞同二楼说的一楼正解,此贴终结。
|
5
Alerta 2018-08-30 00:48:44 +08:00 via iPhone
赞同三楼说的赞同二楼说的一楼正解,此贴终结。
|
6
conn4575 2018-08-30 06:59:19 +08:00 via Android
如果要求保持顺序,list({i:None for i in x}.keys())
|
7
conn4575 2018-08-30 06:59:43 +08:00 via Android
忘记说了,python3
|
12
exhades 2018-08-30 10:08:31 +08:00 via Android
set 就够了
|
13
xinhangliu 2018-08-30 10:53:39 +08:00 via Android
@so1n 指的是 Dict
|
15
so1n 2018-08-30 13:02:55 +08:00
@xinhangliu 3.6 的 dict 变成有序的吗
|
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. |
17
huangke 2018-09-01 16:07:34 +08:00
res = []
lists = = [res.append(i) for i in target if i not in res] |
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] |
19
thautwarm 2018-09-03 17:55:00 +08:00
python sorteddict 的实现已经默认了,原因是这个涉及到 dataclass 和 NamedTuple 的实现。
可以依赖 sorted dict 了 |