Python论坛  - 讨论区

标题:Re: Re: Re:[python-chinese]另外的字典排序问题

2004年09月09日 星期四 09:16

March Liu March.Liu at gmail.com
Thu Sep 9 09:16:33 HKT 2004

咱俩用的方法是一样的。不过,在GMail里你的信怎么和我与朋友的通信混在一起了……这个……看来GMail还是不够稳定啊。


On Thu, 9 Sep 2004 08:36:48 +0800, hoxide <hoxide_dirac at yahoo.com.cn> wrote:
> 在IDLE中:
> >>> aa.sort(lambda x,y: x[1]-y[1])
> >>> a={'a':2, 'b':5, 'c':1}
> >>> b=a.items()
> >>> b.sort(lambda x,y: x[1]-y[1])
> >>> b
> [('c', 1), ('a', 2), ('b', 5)]
> 
> python对列表提供了sort函数,并且是个范函,所以好好利用python内在的能力吧。
> 
> 具体参见Python Library Reference 2.3.6.4 Mutable Sequence Types 注释8:
> 
> 8)
> The sort() method takes an optional argument specifying a comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument. Note that this slows the sorting process down considerably; for example to sort a list in reverse order it is much faster to call sort() followed by reverse() than to use sort() with a comparison function that reverses the ordering of the elements. Passing None as the comparison function is semantically equivalent to calling sort() with no comparison function. Changed in version 2.3: Support for None as an equivalent to omitting cmpfunc was added.
> As an example of using the cmpfunc argument to the sort() method, consider sorting a list of sequences by the second element of that list:
> 
> def mycmp(a, b):
>    return cmp(a[1], b[1])
> 
> mylist.sort(mycmp)
> 
> A more time-efficient approach for reasonably-sized data structures can often be used:
> 
> tmplist = [(x[1], x) for x in mylist]
> tmplist.sort()
> mylist = [x for (key, x) in tmplist]
> 
> ======= 2004-09-08 05:24:32 您在来信中写道:=======
> 
> >要求上光写原始状态是个字典,最后要按正确的顺序输出,没规定必须还是个字典,所以我觉得转移到list里问题不大,所以用list实现了排序。
> >
> >谢谢大家的帮忙。
> >
> >
> >----- Original Message -----
> >From: "GreyRoar" <GreyRoar at tom.com>
> >To: <python-chinese at lists.python.cn>
> >Sent: Tuesday, September 07, 2004 2:00 AM
> >Subject: Re: Re:[python-chinese]另外的字典排序问题
> >
> >
> >Wang Chao,您好!
> >
> >先明确一下你的题目上要求最后结果是存在一个字典吗?还是只说明源是一个字典,结果怎么存随便?如果是后者,那用list只是个过渡。如果是前者,我个人觉得你存结果的字典就得用有序数字做key了,而源字典中key:value则作为目标字典value保存。这样虽然字典本身还是无序的,但表面上你字典的key至少是有序的。
> >
> >
> >======= 2004-09-07 01:03:00 您在来信中写道:=======
> >
> >>那个题目上写明了是要用字典,比较郁闷。能不能把keys和value都弄到list里面,然后按原先的顺序对应好,对这个list排序?
> >>
> >>----- Original Message -----
> >>From: "GreyRoar" <GreyRoar at tom.com>
> >>To: <python-chinese at lists.python.cn>
> >>Sent: Tuesday, September 07, 2004 12:38 AM
> >>Subject: Re: [python-chinese]另外的字典排序问题
> >>
> >>
> >>Wang Chao,您好!
> >>
> >>个人觉得你这样的情况用字典不是特别好,要是用list里面嵌list的办法倒是可以用list.sort(func)的办法按一定条件排序。
> >>
> >>======= 2004-09-06 23:56:00 您在来信中写道:=======
> >>
> >>>现在已经有一个字典,Keys是英文单词,Values是这个单词在文件中出现的次数。
> >>>想要给这个字典重新排序,让出现次数最多的单词排第一位,出现次数最少的排最后。
> >>>
> >>>我尝试把字典里的values放入一个list里,在对list排序,但是好像排完也没有什么意义。很多单词的values值一样,就是排完也没有办法把把排序结果应用回原来的字典里。
> >>>
> >>>我能想当的另一个方案是,每个单词和后面的所有词比较,当遇到values比他大的,就改为values更大的单词开始比较,如果到最后都没有比他大的,就把这个词写入新字典里,同时在旧字典里删除自己,但是好像太弱智了点。
> >>>
> >>>
> >>>不知道还有什么其他更好地解决方案。
> >>>
> >>>谢谢~~~
> >>>
> >>>基础太差,时间又太紧,我问题好多,学得好辛苦 。 :(
> >>>
> >>>
> >>>_______________________________________________
> >>>python-chinese list
> >>>python-chinese at lists.python.cn
> >>>http://python.cn/mailman/listinfo/python-chinese
> >>
> >>= = = = = = = = = = = = = = = = = = = =
> >>
> >>
> >>致
> >>礼!
> >>
> >>
> >>GreyRoar
> >>GreyRoar at tom.com
> >>2004-09-07
> >>
> >>
> >>
> >>
> >>_______________________________________________
> >>python-chinese list
> >>python-chinese at lists.python.cn
> >>http://python.cn/mailman/listinfo/python-chinese
> >>
> >>_______________________________________________
> >>python-chinese list
> >>python-chinese at lists.python.cn
> >>http://python.cn/mailman/listinfo/python-chinese
> >
> >= = = = = = = = = = = = = = = = = = = =
> >
> >
> >致
> >礼!
> >
> >
> >GreyRoar
> >GreyRoar at tom.com
> >2004-09-07
> >
> >
> >
> >
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> >
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> >
> 
> = = = = = = = = = = = = = = = = = = = =
> 
>> 礼!
> 
> hoxide
> hoxide_dirac at yahoo.com.cn
> 2004-09-08
> 
> 
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> 
> 
> 



-- 
刘鑫
March.Liu


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号