码迷,mamicode.com
首页 > 其他好文 > 详细

思考题

时间:2015-08-04 23:15:06      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:enumerate

 有个列表t, 去掉偶数位的值
         t = [5, 6, 7, 8, 9, 10, 11, 12, 13]
          for i, v in enumerate(t):
               if i % 2 == 0:
                   t.remove(v)
          print t   # 请问t是什么




In [1]: t = [5, 6, 7, 8, 9, 10, 11, 12, 13]

In [2]: for i,v in enumerate(t):
   ...:         print "t = %s" % t
   ...:         print "(i,v) = (%d, %d)" % (i,v)
   ...:         if i % 2 == 0:
   ...:                 t.remove(v)
   ...:         print "new_t = %s" % t
   ...:         print "del the value is %d" % v
   ...:         print "================================="
   ...: print "the result of t =  %s" % t



t = [5, 6, 7, 8, 9, 10, 11, 12, 13]
(i,v) = (0, 5)
new_t = [6, 7, 8, 9, 10, 11, 12, 13]
del the value is 5
=================================
t = [6, 7, 8, 9, 10, 11, 12, 13]
(i,v) = (1, 7)
new_t = [6, 7, 8, 9, 10, 11, 12, 13]
del the value is 7
=================================
t = [6, 7, 8, 9, 10, 11, 12, 13]
(i,v) = (2, 8)
new_t = [6, 7, 9, 10, 11, 12, 13]
del the value is 8
=================================
t = [6, 7, 9, 10, 11, 12, 13]
(i,v) = (3, 10)
new_t = [6, 7, 9, 10, 11, 12, 13]
del the value is 10
=================================
t = [6, 7, 9, 10, 11, 12, 13]
(i,v) = (4, 11)
new_t = [6, 7, 9, 10, 12, 13]
del the value is 11
=================================
t = [6, 7, 9, 10, 12, 13]
(i,v) = (5, 13)
new_t = [6, 7, 9, 10, 12, 13]
del the value is 13
=================================
the result of t =  [6, 7, 9, 10, 12, 13]

本文出自 “Linux_Config” 博客,请务必保留此出处http://liang1026.blog.51cto.com/10119067/1681678

思考题

标签:enumerate

原文地址:http://liang1026.blog.51cto.com/10119067/1681678

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!