免费注册 查看新帖 |

Chinaunix

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

使用for循环去除列表中的('')元素,只能去掉一部分 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-17 15:18 |只看该作者 |倒序浏览

  1. >>> line = "a 1 b  2 c   3 d    4"
  2. >>> def f(string):
  3.         string = string.split(' ')
  4.         for i in string:
  5.                 if i == '':
  6.                         string.remove(i)
  7.         print string

  8.        
  9. >>> f(line)
  10. ['a', '1', 'b', '2', 'c', '3', 'd', '', '', '4']
复制代码
这串代码只去掉了列表中的部分('')元素,为什么这样?
谢谢

论坛徽章:
0
2 [报告]
发表于 2012-11-17 17:22 |只看该作者
本帖最后由 aswjh 于 2012-11-17 17:32 编辑

你的逻辑有错误,c后有两个'',d后按空格分有3个'',你用i循环到c后的第一个''时删除了一个,然后你的i会+1,后面的一个同时也会前移,会有漏的
s="a 1 b  2 c   3 d    4"
s.split()
() 内不带符号会以所有空白符作分隔

论坛徽章:
0
3 [报告]
发表于 2012-11-17 18:09 |只看该作者
本帖最后由 106033177 于 2012-11-17 18:10 编辑

回复 1# gleerat
gleerat 发表于 2012-11-17 15:18
        for i in string:
                if i == '':
                        string.remove(i)

不要在迭代列表的同时修改列表,可以迭代列表的副本.
print   [i for  i in line if not i.isspace() ]


   

论坛徽章:
0
4 [报告]
发表于 2012-11-17 21:51 |只看该作者
如果你只是想要获得非空白内容,如下方式即可:
  1. >>> line = "a 1 b  2 c   3 d    4";
  2. >>> splited = line.split();
  3. >>> print splited
  4. ['a', '1', 'b', '2', 'c', '3', 'd', '4']
复制代码
其中,不指定split的分隔符,则会使用默认的方式,去掉所有的空白类字符。
相关语法:

string.split(s[, sep[, maxsplit]])
Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. The optional third argument maxsplit defaults to 0. If it is nonzero, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements).

The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string.

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
5 [报告]
发表于 2012-11-18 10:42 |只看该作者
支持了.

aswjh 发表于 2012-11-17 17:22
你的逻辑有错误,c后有两个'',d后按空格分有3个'',你用i循环到c后的第一个''时删除了一个,然后你的i会+1,后 ...

论坛徽章:
0
6 [报告]
发表于 2012-11-19 20:53 |只看该作者
crifan 发表于 2012-11-17 21:51
如果你只是想要获得非空白内容,如下方式即可:其中,不指定split的分隔符,则会使用默认的方式,去掉所有的 ...


十分感谢。

论坛徽章:
0
7 [报告]
发表于 2012-11-19 20:54 |只看该作者
aswjh 发表于 2012-11-17 17:22
你的逻辑有错误,c后有两个'',d后按空格分有3个'',你用i循环到c后的第一个''时删除了一个,然后你的i会+1,后 ...


谢谢了,明白错在哪里了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP