Python论坛  - 讨论区

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

2005年06月17日 星期五 16:30

Frank gavin at sz.net.cn
Fri Jun 17 16:30:35 HKT 2005

s={'a':1,'b':9,'c':2,'dc':4}

for b,a in [(b,a) for a,b in s.items()]:
   print  a,b
  ----- Original Message ----- 
  From: David 
  To: python-chinese at lists.python.cn 
  Sent: Friday, June 17, 2005 3:35 PM
  Subject: [python-chinese] 字典的排序问题


  请教,我有一个字典,我想按字典的value排序并按序输出key和value应该怎么样实现,谢谢!

   

   



------------------------------------------------------------------------------


  _______________________________________________
  python-chinese list
  python-chinese at lists.python.cn
  http://python.cn/mailman/listinfo/python-chinese
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050617/95cf56d0/attachment.html

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

2005年06月17日 星期五 16:36

Frank gavin at sz.net.cn
Fri Jun 17 16:36:45 HKT 2005

抱歉,前面的有错误,正确的应该为:

s={'a':1,'b':9,'c':2,'dc':4}

s1=[(v,k) for k,v in s.items()]
s1.sort()
for v1,k1 in s1:
   print  k1,v1


  ----- Original Message ----- 
  From: Frank 
  To: python-chinese at lists.python.cn 
  Sent: Friday, June 17, 2005 4:30 PM
  Subject: Re: [python-chinese] 字典的排序问题


  s={'a':1,'b':9,'c':2,'dc':4}

  for b,a in [(b,a) for a,b in s.items()]:
     print  a,b
    ----- Original Message ----- 
    From: David 
    To: python-chinese at lists.python.cn 
    Sent: Friday, June 17, 2005 3:35 PM
    Subject: [python-chinese] 字典的排序问题


    请教,我有一个字典,我想按字典的value排序并按序输出key和value应该怎么样实现,谢谢!

     

     



----------------------------------------------------------------------------


    _______________________________________________
    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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050617/818c785d/attachment.htm

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

2005年06月17日 星期五 16:57

cpunion cpunion at 263.net
Fri Jun 17 16:57:02 HKT 2005

应该要加上个sorted吧:

s = {'a':1, 'b':2, 'c':2, 'd':3, 'e':3, 'f':1}

for b,a in sorted([(b,a) for a,b in s.items()]):
   print  a,b

Frank wrote:

> s={'a':1,'b':9,'c':2,'dc':4}
>  
> for b,a in [(b,a) for a,b in s.items()]:
>    print  a,b
>
>     ----- Original Message -----
>     *From:* David dingyz at szgwbn.net.cn>
>     *To:* python-chinese at lists.python.cn
>     python-chinese at lists.python.cn>
>     *Sent:* Friday, June 17, 2005 3:35 PM
>     *Subject:* [python-chinese] 字典的排序问题
>
>     请教,我有一个字典,我想按字典的value排序并按序输出key和value应该
>     怎么样实现,谢谢!
>
>      
>
>      
>
>     ------------------------------------------------------------------------
>     _______________________________________________
>     python-chinese list
>     python-chinese at lists.python.cn 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
>  
>

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号