简体   繁体   English

从列表中删除仅包含空格的字符串

[英]Remove strings containing only white spaces from list

How do I delete empty strings from a list? 如何从列表中删除空字符串? I tried like this: 我试过这样的:

starring = ['Vashu Bhagnani', 'Khemchand Bhagnani', ' ', 'Jacky Bhagnani', ' ', 'Prashant Shah', ' ']
output = filter(bool, starring)

Output I want: 我想要的输出:

['Vashu Bhagnani', 'Khemchand Bhagnani',  'Jacky Bhagnani',  'Prashant Shah']

But output ends up being the same as input . output最终与input相同。 What's the correct function to pass to filter ? 传递给filter的正确功能是什么?

Only the empty string evaluates to False so you need to use strip() to remove any whitespace and we can then rely on non-blank strings being evaluated as true. 只有空字符串的计算结果为False因此您需要使用strip()来删除任何空格,然后我们可以依赖于非空字符串被评估为true。

>>> starring = ['Vashu Bhagnani', 'Khemchand Bhagnani', ' ', 'Jacky Bhagnani', ' ', 'Prashant Shah', ' ']                                      
>>> starring = filter(lambda name: name.strip(), starring)
>>> starring
['Vashu Bhagnani', 'Khemchand Bhagnani', 'Jacky Bhagnani', 'Prashant Shah']

Although a list comprehension might be easier: 虽然列表理解可能更容易:

>>> [name for name in starring if name.strip()]
['Vashu Bhagnani', 'Khemchand Bhagnani', 'Jacky Bhagnani', 'Prashant Shah']

You can remove trailing and leading white spaces, which will result in an empty string if it only contains those. 您可以删除尾随和前导空格,如果只包含那些空格,则会产生空字符串。

List comprehension: 列表理解:

l = [x for x in l if x.strip()]

With filter and operator.methodcaller [docs] : 使用filteroperator.methodcaller [docs]

l = filter(operator.methodcaller('strip'), l)

or simpler: 或者更简单:

l = filter(str.strip, l)

operator.methodcaller would be the only way if you'd want to pass additional arguments to the method. 如果您想要向该方法传递其他参数, operator.methodcaller将是唯一的方法。

s = ['Vashu Bhagnani', 'Khemchand Bhagnani', '', 'Jacky Bhagnani', '', 'Prashant Shah', '']
[a for a in s if len(a) > 0]

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

相关问题 Python2.7 代码仅从包含 unicode 字符、空格和方括号的列表中提取字符串 - Python2.7 code to extract only strings from list containing unicode characters, blank spaces and square brackets 从字符串列表中删除那些只有空格或由少于 3 个字母数字字符组成的字符串 - Remove from a list of strings, those strings that have only empty spaces or that are made up of less than 3 alphanumeric characters 删除str列表中的空白 - remove white spaces in a list of str 将文件拆分为列表后删除空格 - Remove the white spaces after the splitting the file in to list 是否有一种简单的方法来删除列表项中的空白 - Is there an easy way to remove the white spaces in list items 如何从Python列表中的字符串中删除前导和尾随空格 - How to remove leading and trailing spaces from strings in a Python list 如何更快地从列表中删除包含某些单词的字符串 - How to remove strings containing certain words from list FASTER 从 dict 中删除空格:Python - Remove white spaces from dict : Python 如何从Python中删除字符串中的空格? - How to remove white spaces from a string in Python? 考虑到空格,如何将列表中的精确字符串匹配到更大的字符串? - How do I match exact strings from a list to a larger string taking white spaces into account?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM