Python list的排序

这里主要是用sort方法对list中的数据进行排序。如果list中元素是tuple这样的类型,则按照tuple里面元素的位置的先后来排序。
代码如下:

#coding:utf-8
l = [
(3,'a','john'), (2,'c','allen'), (3,'a','mike'), (6,'b','eddie'), (2,'b','susan')
]

print l
l.sort()
print l

运行结果如下:
C:\>python test.py
[(3, 'a', 'john'), (2, 'c', 'allen'), (3, 'a', 'mike'), (6, 'b', 'eddie'), (2, 'b', 'susan')]
[(2, 'b', 'susan'), (2, 'c', 'allen'), (3, 'a', 'john'), (3, 'a', 'mike'), (6, 'b', 'eddie')]

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Related posts:

  1. Python的lambda函数与排序
  2. Python 3.1.1 RC发布
  3. python元类的一些新认识
  4. Python 多线程 XML RPC的实现
  5. Python CGI实现用户会话
  6. Django at a glance–Django初窥

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu