问题:从字符串列表中删除空字符串
我想从python中的字符串列表中删除所有空字符串。
我的想法如下:
while '' in str_list:
str_list.remove('')
还有其他pythonic方式可以做到这一点吗?
I want to remove all empty strings from a list of strings in python.
My idea looks like this:
while '' in str_list:
str_list.remove('')
Is there any more pythonic way to do this?
回答 0
我会使用filter
:
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))
I would use filter
:
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 returns an iterator from filter
, so should be wrapped in a call to list()
str_list = list(filter(None, str_list))
回答 1
使用列表理解是最Python的方式:
>>> strings = ["first", "", "second"]
>>> [x for x in strings if x]
['first', 'second']
如果必须就地修改列表,因为还有其他引用必须看到更新的数据,则使用分片分配:
strings[:] = [x for x in strings if x]
Using a list comprehension is the most Pythonic way:
>>> strings = ["first", "", "second"]
>>> [x for x in strings if x]
['first', 'second']
If the list must be modified in-place, because there are other references which must see the updated data, then use a slice assignment:
strings[:] = [x for x in strings if x]
回答 2
过滤器实际上对此有一个特殊的选择:
filter(None, sequence)
它将滤除所有评估为False的元素。此处无需使用实际的可调用对象,例如bool,len等。
和map(bool,…)一样快
filter actually has a special option for this:
filter(None, sequence)
It will filter out all elements that evaluate to False. No need to use an actual callable here such as bool, len and so on.
It’s equally fast as map(bool, …)
回答 3
>>> 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
>>> lstr = ['hello', '', ' ', 'world', ' ']
>>> lstr
['hello', '', ' ', 'world', ' ']
>>> ' '.join(lstr).split()
['hello', 'world']
>>> filter(None, lstr)
['hello', ' ', 'world', ' ']
Compare time
>>> 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
Notice that filter(None, lstr)
does not remove empty strings with a space ' '
, it only prunes away ''
while ' '.join(lstr).split()
removes both.
To use filter()
with white space strings removed, it takes a lot more time:
>>> timeit('filter(None, [l.replace(" ", "") for l in lstr])', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000)
18.101892948150635
回答 4
@ Ib33X的回复很棒。如果要删除每个空字符串,请剥离后。您也需要使用strip方法。否则,如果有空格,它将也返回空字符串。如,“”对于该答案也将有效。这样,就可以实现。
strings = ["first", "", "second ", " "]
[x.strip() for x in strings if x.strip()]
答案是["first", "second"]
。
如果要改用filter
method,可以执行like
list(filter(lambda item: item.strip(), strings))
。这给出了相同的结果。
Reply from @Ib33X is awesome. If you want to remove every empty string, after stripped. you need to use the strip method too. Otherwise, it will return the empty string too if it has white spaces. Like, ” ” will be valid too for that answer. So, can be achieved by.
strings = ["first", "", "second ", " "]
[x.strip() for x in strings if x.strip()]
The answer for this will be ["first", "second"]
.
If you want to use filter
method instead, you can do like
list(filter(lambda item: item.strip(), strings))
. This is give the same result.
回答 5
代替if x,我将使用if X!=”来消除空字符串。像这样:
str_list = [x for x in str_list if x != '']
这将在列表中保留“无”数据类型。此外,如果您的列表中有整数,并且0是其中的一个,它也将被保留。
例如,
str_list = [None, '', 0, "Hi", '', "Hello"]
[x for x in str_list if x != '']
[None, 0, "Hi", "Hello"]
Instead of if x, I would use if X != ” in order to just eliminate empty strings. Like this:
str_list = [x for x in str_list if x != '']
This will preserve None data type within your list. Also, in case your list has integers and 0 is one among them, it will also be preserved.
For example,
str_list = [None, '', 0, "Hi", '', "Hello"]
[x for x in str_list if x != '']
[None, 0, "Hi", "Hello"]
回答 6
根据列表的大小,如果您使用list.remove()而不是创建新列表,则可能是最有效的:
l = ["1", "", "3", ""]
while True:
try:
l.remove("")
except ValueError:
break
这具有不创建新列表的优点,但是具有每次都必须从头开始搜索的缺点,尽管与while '' in l
上面建议的用法不同,它每次出现时仅需要搜索一次''
(当然,有一种方法可以保持最佳状态)两种方法,但更为复杂)。
Depending on the size of your list, it may be most efficient if you use list.remove() rather than create a new list:
l = ["1", "", "3", ""]
while True:
try:
l.remove("")
except ValueError:
break
This has the advantage of not creating a new list, but the disadvantage of having to search from the beginning each time, although unlike using while '' in l
as proposed above, it only requires searching once per occurrence of ''
(there is certainly a way to keep the best of both methods, but it is more complicated).
回答 7
请记住,如果要将空格保留在字符串中,则可以使用某些方法无意中将其删除。如果你有这个清单
[‘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]
Keep in mind that if you want to keep the white spaces within a string, you may remove them unintentionally using some approaches. If you have this list
[‘hello world’, ‘ ‘, ”, ‘hello’] what you may want [‘hello world’,’hello’]
first trim the list to convert any type of white space to empty string:
space_to_empty = [x.strip() for x in _text_list]
then remove empty string from them list
space_clean_list = [x for x in space_to_empty if x]
回答 8
用途filter
:
newlist=filter(lambda x: len(x)>0, oldlist)
如所指出的,使用过滤器的缺点是它比替代方法慢。而且,lambda
通常很昂贵。
或者,您可以选择最简单,最迭代的方法:
# 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
这是最直观的方法,并且可以在适当的时间内完成。
Use filter
:
newlist=filter(lambda x: len(x)>0, oldlist)
The drawbacks of using filter as pointed out is that it is slower than alternatives; also, lambda
is usually costly.
Or you can go for the simplest and the most iterative of all:
# 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
this is the most intuitive of the methods and does it in decent time.
回答 9
正如Aziz Alto 所报告的filter(None, lstr)
那样,不会删除带有空格的空字符串,' '
但是如果您确定lstr仅包含字符串,则可以使用filter(str.strip, 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']
您会看到filter(str.strip, lstr)
保留带空格的字符串,但' '.join(lstr).split()
会拆分这些字符串。
As reported by Aziz Alto filter(None, lstr)
does not remove empty strings with a space ' '
but if you are sure lstr contains only string you can use filter(str.strip, lstr)
>>> lstr = ['hello', '', ' ', 'world', ' ']
>>> lstr
['hello', '', ' ', 'world', ' ']
>>> ' '.join(lstr).split()
['hello', 'world']
>>> filter(str.strip, lstr)
['hello', 'world']
Compare time on my pc
>>> 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
The fastest solution to remove ''
and empty strings with a space ' '
remains ' '.join(lstr).split()
.
As reported in a comment the situation is different if your strings contain spaces.
>>> 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']
You can see that filter(str.strip, lstr)
preserve strings with spaces on it but ' '.join(lstr).split()
will split this strings.
回答 10
总结最佳答案:
1.消除空洞而无需剥离:
也就是说,保留所有空格字符串:
slist = list(filter(None, slist))
优点:
2.去除剥离后的空容器…
2.a …当字符串在单词之间不包含空格时:
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)
Sum up best answers:
1. Eliminate emtpties WITHOUT stripping:
That is, all-space strings are retained:
slist = list(filter(None, slist))
PROs:
- simplest;
- fastest (see benchmarks below).
2. To eliminate empties after stripping …
2.a … when strings do NOT contain spaces between words:
slist = ' '.join(slist).split()
PROs:
- small code
- fast (BUT not fastest with big datasets due to memory, contrary to what @paolo-melchiorre results)
2.b … when strings contain spaces between words?
slist = list(filter(str.strip, slist))
PROs:
- fastest;
- understandability of the code.
Benchmarks on a 2018 machine:
## 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)
回答 11
对于包含空格和空值的列表,请使用简单的列表理解-
>>> s = ['I', 'am', 'a', '', 'great', ' ', '', ' ', 'person', '!!', 'Do', 'you', 'think', 'its', 'a', '', 'a', '', 'joke', '', ' ', '', '?', '', '', '', '?']
因此,您可以看到,此列表包含空格和null元素的组合。使用摘要-
>>> d = [x for x in s if x.strip()]
>>> d
>>> d = ['I', 'am', 'a', 'great', 'person', '!!', 'Do', 'you', 'think', 'its', 'a', 'a', 'joke', '?', '?']
For a list with a combination of spaces and empty values, use simple list comprehension –
>>> s = ['I', 'am', 'a', '', 'great', ' ', '', ' ', 'person', '!!', 'Do', 'you', 'think', 'its', 'a', '', 'a', '', 'joke', '', ' ', '', '?', '', '', '', '?']
So, you can see, this list has a combination of spaces and null elements. Using the snippet –
>>> d = [x for x in s if x.strip()]
>>> d
>>> d = ['I', 'am', 'a', 'great', 'person', '!!', 'Do', 'you', 'think', 'its', 'a', 'a', 'joke', '?', '?']
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。