2005年06月17日 星期五 16:30
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
2005年06月17日 星期五 16:36
抱歉,前面的有错误,正确的应该为:
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
2005年06月17日 星期五 16:57
应该要加上个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
>
>
Zeuux © 2025
京ICP备05028076号