Dedupe and sort list in Python 2.2

In Python 2.2 (don't ask) what is the easiest way to sort the list and remove duplicates?

I obviously can write a function that sort() then repeat, but I wonder if there is an idiomatic single-line font.

edit: The list is short, so efficiency is not a concern. In addition, the elements are immutable.

+6
source share
4 answers

For older versions of python and since you use strings, there is not a single liner that I can think of, but the template will probably be like this using dictionaries:

 def sorted_uniq(your_list): table = {} for s in your_list: table[s] = None k = table.keys() k.sort() return k 

Adapted from the ancient stream of ActiveState code fragments, which Alex Martelli himself wrote several comments: http://code.activestate.com/recipes/52560/

A shorter way with a list:

 def sort_uniq(alist): d = {} mod_list = [d.setdefault(i,i) for i in alist if i not in d] mod_list.sort() return mod_list 

Besides Stephen, a neat (albeit slightly unattractive) one liner, I think it is heading for the smallest lines and the most idiomatic way to do this with Python 2.2:

Thanks to Steven Rumbalski in the comments, the second version can be further compressed using the python zip function:

 def sort_uniq(alist): mod_list = dict(zip(alist,alist)).keys() mod_list.sort() return mod_list 

If list.sort() does not work by side effect, we will have one liner .;)

+7
source

Idiomatic and single line? Not.

Here's a non-idiomatic butt-ugly single-line layer.

 >>> x = [4, 3, 3, 2, 4, 1] >>> [y for y in (locals().__setitem__('d',{}) or x.sort() or x) if y not in d and (d.__setitem__(y, None) or True)] [1, 2, 3, 4] 

If a simple two-line line is acceptable:

 x = [4, 3, 3, 2, 4, 1] x = dict(map(None,x,[])).keys() x.sort() 

Or create two small helper functions (works for any sequence):

 def unique(it): return dict(map(None,it,[])).keys() def sorted(it): alist = [item for item in it] alist.sort() return alist print sorted(unique([4, 3, 3, 2, 4, 1])) 

gives

 [1, 2, 3, 4] 

And finally, a semi-pyphonic one liner:

 x = [4, 3, 3, 2, 4, 1] x.sort() or [s for s, t in zip(x, x[1:] + [None]) if s != t] 
+5
source

Python 2.2 has sets for the record, but in the β€œsets” module, so this will give you a long way to go:

 from sets import Set myList = list(Set(myList)) # now we're duplicate-free, a standard sorting might be enough myList.sort() 
+2
source

Probably the best answer is to use a binary tree:

 # Make yield work in Python 2.2 from __future__ import generators class TreeNode(object): def __init__(self, value): self.left = None self.right = None self.value = value def add(self, value): if value == self.value: return if value < self.value: if self.left is None: self.left = TreeNode(value) else: self.left.add(value) else: if self.right is None: self.right = TreeNode(value) else: self.right.add(value) def __iter__(self): if self.left is not None: for value in self.left: yield value yield self.value if self.right is not None: for value in self.right: yield value class DedupeSorter(object): def __init__(self): self.root = None def add(self, value): if self.root is None: self.root = TreeNode(value) else: self.root.add(value) def __iter__(self): if self.root is None: return [] else: return self.root.__iter__() def dedupe_and_sort(l): sorter = DedupeSorter() for value in l: sorter.add(value) return list(sorter) 

Definitely not idiomatic, but should be fast. It basically creates a tree set and iterates over it. I don't have Python 2.2, hope it works: p

0
source

Source: https://habr.com/ru/post/899317/


All Articles