我想从python中的字符串列表中删除所有空字符串。
我的想法是这样的:
while '' in str_list:
str_list.remove('')
还有什么更python化的方法吗?
我想从python中的字符串列表中删除所有空字符串。
我的想法是这样的:
while '' in str_list:
str_list.remove('')
还有什么更python化的方法吗?
使用列表推导式是最python化的方式:
>>> strings = ["first", "", "second"]
>>> [x for x in strings if x]
['first', 'second']
如果列表必须就地修改,因为有其他引用必须看到更新的数据,那么使用slice赋值:
strings[:] = [x for x in strings if x]
我会使用滤镜:
str_list = filter(None, str_list)
str_list = filter(bool, str_list)
str_list = filter(len, str_list)
str_list = filter(lambda item: item, str_list)
Python 3从filter返回一个迭代器,因此应该包装在调用list()中
str_list = list(filter(None, str_list))
根据列表的大小,使用list.remove()而不是创建一个新列表可能是最有效的:
l = ["1", "", "3", ""]
while True:
try:
l.remove("")
except ValueError:
break
这样做的优点是不需要创建一个新的列表,但缺点是每次都必须从头搜索,尽管不像上面所建议的在l中使用while“每次只需要搜索一次”(当然有一种方法可以保持两种方法的优点,但它更复杂)。
Filter实际上有一个特殊的选项:
filter(None, sequence)
它将过滤掉所有值为False的元素。这里不需要使用一个实际的可调用对象,比如bool, len等等。
它和map(bool,…)一样快
使用过滤器:
newlist=filter(lambda x: len(x)>0, oldlist)
使用过滤器的缺点是,它比替代品慢;而且,通常是昂贵的。
或者你也可以选择最简单且迭代性最强的方法:
# I am assuming listtext is the original list containing (possibly) empty items
for item in listtext:
if item:
newlist.append(str(item))
# You can remove str() based on the content of your original list
这是最直观的方法,并在适当的时间。
为了消除空字符串,我将使用if x != "而不是if x。是这样的:
str_list = [x for x in str_list if x != '']
这将在列表中保留None数据类型。此外,如果您的列表中有整数,0是其中之一,它也将被保留。
例如,
str_list = [None, '', 0, "Hi", '', "Hello"]
[x for x in str_list if x != '']
[None, 0, "Hi", "Hello"]
>>> lstr = ['hello', '', ' ', 'world', ' ']
>>> lstr
['hello', '', ' ', 'world', ' ']
>>> ' '.join(lstr).split()
['hello', 'world']
>>> filter(None, lstr)
['hello', ' ', 'world', ' ']
比较的时间
>>> from timeit import timeit
>>> timeit('" ".join(lstr).split()', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000)
4.226747989654541
>>> timeit('filter(None, lstr)', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000)
3.0278358459472656
注意,filter(None, lstr)不会删除带有空格''的空字符串,它只会删除'',而'' .join(lstr).split()会删除两者。
要使用filter()删除空白字符串,需要更多的时间:
>>> timeit('filter(None, [l.replace(" ", "") for l in lstr])', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000)
18.101892948150635
来自@Ib33X的回复太棒了。如果您想删除每个空字符串,剥离后。你还需要使用条带法。否则,如果字符串中有空格,它也会返回空字符串。比如," "对于这个答案也是有效的。所以,可以通过。
strings = ["first", "", "second ", " "]
[x.strip() for x in strings if x.strip()]
这个问题的答案是["first", "second"]。 如果你想用滤镜方法代替,你可以这样做 List (filter(lambda item: item.strip(), strings))。这是相同的结果。
正如Aziz Alto过滤器(None, lstr)所报道的那样,不会用空格' '删除空字符串,但如果你确定lstr只包含字符串,则可以使用过滤器(str.;)地带,lstr)
>>> lstr = ['hello', '', ' ', 'world', ' ']
>>> lstr
['hello', '', ' ', 'world', ' ']
>>> ' '.join(lstr).split()
['hello', 'world']
>>> filter(str.strip, lstr)
['hello', 'world']
在我的电脑上比较时间
>>> from timeit import timeit
>>> timeit('" ".join(lstr).split()', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000)
3.356455087661743
>>> timeit('filter(str.strip, lstr)', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000)
5.276503801345825
最快的解决方案删除“和空字符串的空格''保留'' .join(lstr).split()。
正如在评论中报告的那样,如果字符串包含空格,情况就不同了。
>>> lstr = ['hello', '', ' ', 'world', ' ', 'see you']
>>> lstr
['hello', '', ' ', 'world', ' ', 'see you']
>>> ' '.join(lstr).split()
['hello', 'world', 'see', 'you']
>>> filter(str.strip, lstr)
['hello', 'world', 'see you']
你可以看到过滤器(str。Strip, lstr)保留带有空格的字符串,但' '.join(lstr).split()将分割该字符串。
总结最佳答案:
1. 消除空带而不剥离:
也就是说,全空间字符串被保留:
slist = list(filter(None, slist))
优点:
简单的; 最快(参见下面的基准测试)。
2. 消除空后剥离…
2.一个……当字符串之间不包含空格时:
slist = ' '.join(slist).split()
优点:
小的代码 快 (但与@paolo-melchiorre的结果相反,由于内存的原因,在大数据集上不是最快的)
2.b…当字符串在单词之间包含空格?
slist = list(filter(str.strip, slist))
优点:
最快; 代码的可理解性。
2018年机器的基准测试:
## Build test-data
#
import random, string
nwords = 10000
maxlen = 30
null_ratio = 0.1
rnd = random.Random(0) # deterministic results
words = [' ' * rnd.randint(0, maxlen)
if rnd.random() > (1 - null_ratio)
else
''.join(random.choices(string.ascii_letters, k=rnd.randint(0, maxlen)))
for _i in range(nwords)
]
## Test functions
#
def nostrip_filter(slist):
return list(filter(None, slist))
def nostrip_comprehension(slist):
return [s for s in slist if s]
def strip_filter(slist):
return list(filter(str.strip, slist))
def strip_filter_map(slist):
return list(filter(None, map(str.strip, slist)))
def strip_filter_comprehension(slist): # waste memory
return list(filter(None, [s.strip() for s in slist]))
def strip_filter_generator(slist):
return list(filter(None, (s.strip() for s in slist)))
def strip_join_split(slist): # words without(!) spaces
return ' '.join(slist).split()
## Benchmarks
#
%timeit nostrip_filter(words)
142 µs ± 16.8 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit nostrip_comprehension(words)
263 µs ± 19.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit strip_filter(words)
653 µs ± 37.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit strip_filter_map(words)
642 µs ± 36 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit strip_filter_comprehension(words)
693 µs ± 42.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit strip_filter_generator(words)
750 µs ± 28.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit strip_join_split(words)
796 µs ± 103 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
请记住,如果您想在字符串中保留空白,您可能会使用某些方法无意中删除它们。 如果你有这个列表
['hello world', '', ", 'hello'] 你可能想要['hello world','hello']
首先修剪列表,将任何类型的空白转换为空字符串:
space_to_empty = [x.strip() for x in _text_list]
然后从列表中删除空字符串
space_clean_list = [x for x in space_to_empty if x]
使用正则表达式和筛选器进行匹配
lstr = ['hello', '', ' ', 'world', ' ']
r=re.compile('^[A-Za-z0-9]+')
results=list(filter(r.match,lstr))
print(results)