繁体   English   中英

如何删除列表中的空格?

[英]How to remove empty spaces in list?

我有一段文字:

text = '''
    Wales greatest moment. Lille is so close to the Belgian 
    border, 
    this was essentially a home game for one of the tournament favourites. Their 
    confident supporters mingled with their new Welsh fans on the streets, 
    buying into the carnival spirit - perhaps more relaxed than some might have 
    been before a quarter-final because they thought this was their time.
    In the driving rain, Wales produced the best performance in their history to 
    carry the nation into uncharted territory. Nobody could quite believe it.'''

我有一个代码:

 words = text.replace('.',' ').replace(',',' ').replace('\n',' ').split(' ')
    print(words)

和输出:

['Wales', 'greatest', 'moment', '', 'Lille', 'is', 'so', 'close', 'to', 'the', 'Belgian', 'border', '', '', 'this', 'was', 'essentially', 'a', 'home', 'game', 'for', 'one', 'of', 'the', 'tournament', 'favourites', '', 'Their', '', 'confident', 'supporters', 'mingled', 'with', 'their', 'new', 'Welsh', 'fans', 'on', 'the', 'streets', '', '', 'buying', 'into', 'the', 'carnival', 'spirit', '-', 'perhaps', 'more', 'relaxed', 'than', 'some', 'might', 'have', '', 'been', 'before', 'a', 'quarter-final', 'because', 'they', 'thought', 'this', 'was', 'their', 'time', '', 'In', 'the', 'driving', 'rain', '', 'Wales', 'produced', 'the', 'best', 'performance', 'in', 'their', 'history', 'to', '', 'carry', 'the', 'nation', 'into', 'uncharted', 'territory', '', 'Nobody', 'could', 'quite', 'believe', 'it', '']

你可以看到,列表中有空格,我删除了'\\n'',''.' .

但现在我不知道如何删除这些空格。

你可以过滤它们,如果你不喜欢它们

no_empties = list(filter(None, words))

如果 function 是None ,则假定恒等函数,即删除所有为 false 的 iterable 元素。

这是有效的,因为空元素被认为是错误的。

编辑:

由于破折号,原始答案不会产生与评论中提到的相同的输出,以避免出现这种情况:

import re
words = re.findall(r'[\w-]+', text)

原答案

你可以通过re模块直接得到你想要的

import re
words = re.findall(r'\w+', text)


['Wales',
 'greatest',
 'moment',
 'Lille',
 'is',
 'so',
 'close',
 'to',
 'the',
 'Belgian',
 'border',
 'this',
 'was',
 'essentially',
 'a',
 'home',
 'game',
 'for',
 'one',
 'of',
 'the',
 'tournament',
 'favourites',
 'Their',
 'confident',
 'supporters',
 'mingled',
 'with',
 'their',
 'new',
 'Welsh',
 'fans',
 'on',
 'the',
 'streets',
 'buying',
 'into',
 'the',
 'carnival',
 'spirit',
 'perhaps',
 'more',
 'relaxed',
 'than',
 'some',
 'might',
 'have',
 'been',
 'before',
 'a',
 'quarter',
 'final',
 'because',
 'they',
 'thought',
 'this',
 'was',
 'their',
 'time',
 'In',
 'the',
 'driving',
 'rain',
 'Wales',
 'produced',
 'the',
 'best',
 'performance',
 'in',
 'their',
 'history',
 'to',
 'carry',
 'the',
 'nation',
 'into',
 'uncharted',
 'territory',
 'Nobody',
 'could',
 'quite',
 'believe',
 'it']

您遇到此问题的原因是您的文本值在每行中缩进了 4 个空格,而不是因为您的代码有缺陷。 如果您的意思是每行有 4 个空格,您可以将.replace(' ','')到您的 'words' 逻辑中以解决此问题,或者您可以参考 Thomas Weller 的解决方案,无论有多少都可以解决问题你离开的连续单个空格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM