首页 > 其他 > 详细

思考题

时间:2015-08-04 23:13:34      阅读:272      评论:0      收藏:0      [点我收藏+]

 有个列表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

思考题

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!