免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 33187 | 回复: 12
打印 上一主题 下一主题

有关list遍历的习惯 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-26 08:31 |只看该作者 |倒序浏览
最近写一个东西,感觉从头到尾写得最多的就是for x in range(len(xlist)):...
in生成器的做法确实很方便,但不能得到索引数字——似乎可以用b_index = xlist.index(x)这样的做法,但我不知道这样做对性能影响大不大,特别是当数组非常大的时候。

不知各位有没更好的方法,讨论下。
3751 该用户已被删除
2 [报告]
发表于 2007-05-26 10:46 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
3 [报告]
发表于 2007-05-27 20:56 |只看该作者
ll = [1, 2, 3, 4]
for i in ll:
    print i

论坛徽章:
0
4 [报告]
发表于 2007-05-28 09:00 |只看该作者
还可以使用xrange来生成序列。

论坛徽章:
0
5 [报告]
发表于 2007-05-28 09:26 |只看该作者
原帖由 zqc53 于 2007-5-26 08:31 发表
最近写一个东西,感觉从头到尾写得最多的就是for x in range(len(xlist)):...
in生成器的做法确实很方便,但不能得到索引数字——似乎可以用b_index = xlist.index(x)这样的做法,但我不知道这样做对性能影响大 ...


我感觉用 for x in range.... 这样的,对性能还是有点影响,
很可能是需要先生成一个list,
前段时间我在用for x in range(1000000000) 的时候,居然把solaris给跑的所有fork都出错。。。

论坛徽章:
0
6 [报告]
发表于 2007-05-28 09:29 |只看该作者
range占用内存要比xrange大得多,所以建议使用xrange

论坛徽章:
0
7 [报告]
发表于 2007-05-28 10:01 |只看该作者
This function is very similar to range(), but returns an ``xrange object'' instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range's elements are never used (such as when the loop is usually terminated with break).
Note: xrange() is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs ("short" Python integers), and also requires that the number of elements fit in a native C long.


说的很清楚了!
3751 该用户已被删除
8 [报告]
发表于 2007-05-28 12:18 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
9 [报告]
发表于 2007-05-29 18:48 |只看该作者
list要删除里面的某些元素,怎么做?

一个简单的代码就是这样:

  1. l = [1, 2, 3, 3, 3]

  2. for i in l:
  3.     if i == 3:
  4.         l.remove(i)

  5. print l
复制代码


但结果不对,    显示的是   [1, 2, 3]

那好,换个删法

  1. l = [1, 2, 3, 3, 3]

  2. for i in range(len(l)):
  3.     if l[i] == 3:
  4.         del l[i]

  5. print l
复制代码


结果跟惨,直接抛IndexError了

再试一个

  1. l = [1, 2, 3, 3, 3]

  2. for i in range(len(l)-1, -1, -1):
  3.     if l[i] == 3:
  4.         del l[i]

  5. print l
复制代码


噢,这次可以了, 但这种方法并不是很漂亮.
再用个漂亮的方法吧, python手册推荐的
  1. l = [1, 2, 3, 3, 3]

  2. l = filter(lambda x: x != 3, l)
  3. print l

复制代码


倒序删除可以成功,而其他的遍历删除法都会出错.
说明了一旦你进入了list的遍历循环中,再对这个list做操作时一定要小心,很多错误看起来并不明显.
最好使用用系统给的那些函数,filter, reduce, map

[ 本帖最后由 xxandxx 于 2007-5-29 18:49 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2007-05-29 22:24 |只看该作者
python不是推荐用[]替代filter么
l = [1, 2, 3, 3, 3]
l=[i for i in l if i!=3]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP