在 Python 2.2 中对列表进行重复数据删除和排序

发布于 2024-12-10 02:30:16 字数 192 浏览 1 评论 0原文

在Python 2.2(不要问)中,对列表进行排序和删除重复项的最简洁方法是什么?

显然,我可以编写一个函数,该函数将 sort() 然后进行迭代,但我想知道是否有一个惯用的单行代码。

编辑:列表很短,因此效率不是问题。此外,元素是不可变的。

In Python 2.2 (don't ask), what's the neatest way to sort a list and remove duplicates?

I can obviously write a function that would sort() then iterate, but am wondering if there's an idiomatic one-liner.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔少女心 2024-12-17 02:30:16

对于旧的 python 版本,由于您使用的是字符串,所以我无法想到单行代码,但使用字典的模式可能是这样的:

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

改编自古老的 ActiveState 代码片段线程,Alex Martelli 本人在其中写了几条评论: http://code.activestate.com/recipes/52560/

一种更短的方法列表推导式:

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

除了 Steven 简洁的(但有点不吸引人的)一行,我认为这将朝着使用 Python 2.2 最少的行数和最惯用的方式进行:

感谢 Steven Rumbalski 的评论,第二个版本可以进一步压缩为python 的 zip 函数:

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

如果 list.sort() 没有副作用,我们就会有一个衬垫。 ;)

For old python versions, and since you're using strings, there's no one-liner I can think of, but a pattern would probably be 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 an ancient ActiveState code snippet thread that Alex Martelli himself wrote several comments on: http://code.activestate.com/recipes/52560/

A shorter way with list comprehensions:

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

Aside from Steven's neat (yet slightly unattractive) one liner, I think this heads toward the fewest lines and most idiomatic way of doing it with Python 2.2:

Thanks to Steven Rumbalski in the comments, the 2nd version can be condensed further with python's zip function:

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

If list.sort() didn't operate by side effect, we'd have a one liner. ;)

木落 2024-12-17 02:30:16

惯用语和一句台词?不,

这是一句不惯用的、丑陋的俏皮话。

>>> 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]

如果一个简单的两行代码是可以接受的:

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

或者制作两个小辅助函数(适用于任何序列):

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]))

给出

[1, 2, 3, 4]

最后,一个半Python式的单行代码:

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

Idiomatic and a one-liner? No.

Here's a non-idiomatic butt-ugly one-liner.

>>> 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-liner is acceptable:

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

Or make 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 semipythonic 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]
瞄了个咪的 2024-12-17 02:30:16

根据记录,Python 2.2 确实有集合,但是在“集合”模块下,所以这会给你带来很大的帮助:

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

For the record, Python 2.2 does have sets, but under the "sets" module, so this will get you a long way:

from sets import Set
myList = list(Set(myList))
# now we're duplicate-free, a standard sorting might be enough
myList.sort()
放赐 2024-12-17 02:30:16

也许最好的答案是使用二叉树:

# 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)

绝对不惯用,但应该很快。它基本上创建一个基于树的集合并对其进行迭代。我没有 Python 2.2 所以希望它能工作:p

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-based set and iterates over it. I don't have Python 2.2 so hopefully it works :p

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文