确定序列排序的最佳方法? [英] best way to determine sequence ordering?

查看:90
本文介绍了确定序列排序的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想列出四个项目,例如L = [''C'','A'',''D'',''B''],

然后弄清楚某个元素是否在另一个元素之前,是什么

是最好的方法吗?


看看内置的列表函数,我想我可以这样做:
<如果L.index(''A'')< b>
L.index(''D''):

#做一些东西


但是我不知道是否有一个首选的方法这种类型的

操作,或者可能是一个更好的功能来用来计算

的顺序。或者即使我想编写自己的实用函数来制作

它看起来比上面更清晰,我仍然需要在我的函数中使用类似上面的东西

,所以我想知道什么可能是''最干净''寻找

解决方案。


谢谢。

If I want to make a list of four items, e.g. L = [''C'', ''A'', ''D'', ''B''],
and then figure out if a certain element precedes another element, what
would be the best way to do that?

Looking at the built-in list functions, I thought I could do something like:

if L.index(''A'') < L.index(''D''):
# do some stuff

But I didn''t know if maybe there was a preferred method for this type of
operation, or perhaps a better function to use to figure out the
ordering. Or even if I wanted to write my own utility function to make
it look cleaner than above, I''d still need to use something like above
in my function, so I wanted to know what might be the ''cleanest'' looking
solution.

Thanks.

推荐答案

索引是关于你正在使用的结构所能做的最好的。

如果你做了项目一个类的实例,然后你可以定义一个

__cmp__成员,这将让你这样做:


a = Item(''A'')

b = Item(''B'')

如果a< b:something


Item类可以使用各种方法维持订单

信息。如果没有太多的值,它可以有一个

字典存储订单的整数:


class Item(object):

def __init __(self,value):

self.val = value

self.order = dict(c = 0,a = 1,d = 2 ,b = 3)

def __cmp __(自我,其他):

返回cmp(self.order [self.val],self.order [other.val])


如果你不关心表现,或者你发现它更清楚,只需使用:

self.order = [''C'', ''A'',''D'',''B'']



def __cmp __(自我,其他):

返回cmp(self.order.index(self.value),

self.order.index(other.value))

- George Young

index is about the best you can do with the structure you''re using.
If you made the "items" instances of a class, then you could define a
__cmp__ member, which would let you do:

a=Item(''A'')
b=Item(''B'')
if a<b: something

The Item class could use any of various means to maintain order
information. If there are not too many values, it could have a
dictionary storing an integer for the order:

class Item(object):
def __init__(self, value):
self.val=value
self.order = dict(c=0, a=1, d=2, b=3)
def __cmp__(self, other):
return cmp(self.order[self.val], self.order[other.val])

If you don''t care about performance, or you find it clearer, just use:
self.order = [''C'', ''A'', ''D'', ''B'']
and
def __cmp__(self, other):
return cmp(self.order.index(self.value),
self.order.index(other.value))
-- George Young


gr*@ll.mit.edu 写道:
索引是关于你正在使用的结构所能做的最好的。
如果你制作了项目。一个类的实例,然后你可以定义一个
__cmp__成员,它可以让你这样做:

a = Item(''A'')
b = Item(' 'B'')
如果< b:something

Item类可以使用各种方法维护订单信息。如果没有太多的值,它可能有一个存储整数的字典:

类Item(object):
def __init __(self,value):
self.val = value
self.order = dict(c = 0,a = 1,d = 2,b = 3)
def __cmp __(self,other):
return cmp(self.order [self.val],self.order [other.val])

如果你不关心表现,或者你发现它更清楚,只需使用:
self.order = [''C'',''A'',''D'',''B'']

def __cmp __(自我,其他):
返回cmp(self.order.index(self.value),
self.order.index(other.value))

- George Young
index is about the best you can do with the structure you''re using.
If you made the "items" instances of a class, then you could define a
__cmp__ member, which would let you do:

a=Item(''A'')
b=Item(''B'')
if a<b: something

The Item class could use any of various means to maintain order
information. If there are not too many values, it could have a
dictionary storing an integer for the order:

class Item(object):
def __init__(self, value):
self.val=value
self.order = dict(c=0, a=1, d=2, b=3)
def __cmp__(self, other):
return cmp(self.order[self.val], self.order[other.val])

If you don''t care about performance, or you find it clearer, just use:
self.order = [''C'', ''A'', ''D'', ''B'']
and
def __cmp__(self, other):
return cmp(self.order.index(self.value),
self.order.index(other.value))
-- George Young




谢谢。随着我的小项目的进展,我开始想要上课了,所以如果我将b $ b兑换成它,你的建议可能会有所帮助。



Thanks. As I progressed with my little project, I was beginning to
wonder about making a class, so your suggestions might be helpful if I
convert it to that.


gr *@ll.mit.edu 写道:
class Item(object):
def __init __(self,value):
self.val = value
self.order = dict(c = 0,a = 1,d = 2, b = 3)
def __cmp __(self,other):
返回cmp(self.order [self.val],self.order [other.val])
class Item(object):
def __init__(self, value):
self.val=value
self.order = dict(c=0, a=1, d=2, b=3)
def __cmp__(self, other):
return cmp(self.order[self.val], self.order[other.val])




一个跟踪它存储在外部容器中的顺序的对象

对我来说似乎是低音 - 如果你原谅这个表达式的话。你怎么用
更新订单?为什么每个对象都需要随身携带全球

订购?如果您需要两个单独的排序,例如a,b,c,d和

a,c,d,b,会发生什么?并且它并不清楚<运营商正在比较

您的例子:


a =项目(''A'')

b =项目(''B '')

如果a< b:东西


我会将订购逻辑放在容器中。这样的事情:


class mylist(list):

def before(me,a,b):

返回我.index(a)< me.index(b)


l = mylist([''C'',''A'',''D'',''B''])
如果l.before('''',''D''):

一些东西


这看起来更清晰,更灵活。当a或b不在列表中时,你仍然需要处理

例外。



An object that keeps track of the order it''s stored in an external container
seems bass-ackwards to me (if you''ll pardon the expression). How do you
update the order? Why does every object need to carry around a global
ordering? What happens if you need two separate orderings like a,b,c,d and
a,c,d,b? And it isn''t clear at all what the < operator is comparing in
your example:

a=Item(''A'')
b=Item(''B'')
if a < b: something

I''d put the ordering logic in the container instead. Something like this:

class mylist(list):
def before (me, a, b):
return me.index(a) < me.index(b)

l = mylist ([''C'', ''A'', ''D'', ''B''])
if l.before(''A'', ''D''):
something

This seems clearer and more flexible. You''ll still have to handle
exceptions when a or b aren''t in the list.


这篇关于确定序列排序的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆