Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-删除列表中具有多个空格的项_Python - Fatal编程技术网

Python-删除列表中具有多个空格的项

Python-删除列表中具有多个空格的项,python,Python,假设我有一个包含多个字符串的列表。 例如: ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i'] 有没有办法删除或弹出字符串中有多个空格的所有项目?因此,输出将是: ['dirty room', 'dormitory'] result=[l代表测试列表中的l,如果l.count(“”

假设我有一个包含多个字符串的列表。 例如:

['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
有没有办法删除或弹出字符串中有多个空格的所有项目?因此,输出将是:

['dirty room', 'dormitory']
result=[l代表测试列表中的l,如果l.count(“”)<2]

使用列表理解和筛选:

old = ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
new = [item for item in old if item.count(' ') < 2]
old=['dirty room'、'do to i'、'summy'、'dry to or i'、'i to rod'、'or to i dry'、'rod to i'、'room i dry'、'root i dry'、'to rod i']
new=[旧if item.count(“”)<2]中的项对应项]
您可以使用过滤器:

my_list=['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
result = list(filter(lambda x: x.count(' ') < 2, my_list))
my_list=[“脏房间”、“对我做”、“宿舍”、“对我干”、“对我干”、“对我干”、“对我干”、“对我干”、“房间我干”、“根我干”、“对我干”]
结果=列表(过滤器(λx:x.count(“”)<2,我的列表))
TL;博士 使用列表理解

列表理解的替代方法 如果我必须按面值接受OP的“…删除或弹出所有项目 有了……我会这样继续

# INITIALIZATION
n = len(strings)
i_del = 0 # how many items have I deleted till now?

for i in range(n):
    if condition(strings[i-i_del]):
        strings.pop(i-i_del)
        i_del = i_del+1
或者像这样,倒计时从我们的 (简单)簿记

n = len(strings)
for i in range(1,n+1):
    if condition(strings[n-i]) : strings.pop(n-i)

关于列表循环的浅见
  • 当您只想使用 已存储到列表中的值

  • 你必须在列表的索引上使用一个简单的循环,
    对于范围内的i(len(my_列表)):…
    当您要修改 值存储到列表中,因为简单值是不可变的

  • 您必须循环索引并采取额外措施(继续 向后,使用辅助计数器等) 通过删除值或插入新值来修改列表本身 将价值观融入其中


  • 速度 仅供参考,它的速度与汽车的速度不相上下 列出其他地方提出的理解

    In [27]: from random import randint as r
    
    In [28]: test = [''.join(chr(r(65,90)) for _ in range(r(11,20))) for count in range(100)]
    
    In [29]: %timeit new = [i for i in test if i.count('A')>1]
    10000 loops, best of 3: 31.3 µs per loop
    
    In [30]: %timeit t2 = test[:]
    The slowest run took 5.44 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000000 loops, best of 3: 542 ns per loop
    
    In [31]: %%timeit
        ...: t2 = test[:]
        ...: for i in range(1,101):
        ...:     if t2[100-i].count('A')>1 : t2.pop(100-i)
        ...: 
    10000 loops, best of 3: 41 µs per loop
    
    考虑到复制
    test
    的成本可以忽略不计,我想说
    .pop()
    比列表理解慢30%


    提防速度测试 当然,如果我选择了一个有更多
    pop
    s的测试,我会倾向于 列表理解能力会更强,但我选择测试什么。
    寓意:提防速度测试…

    一个使用
    regex
    列表理解的简单解决方案:

    given = ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
    result = [i for i in given if len(re.findall(" ", i)) < 2]
    
    given=['dirty room'、'do to i'、'summy'、'dry to or i'、'i to rod'、'or to i dry'、'rod to i'、'room i dry'、'root i dry'、'to rod i']
    结果=[如果len(re.findall(“,i))<2,则给出i表示i]
    

    别忘了先导入re

    一个简单的方法来实现:

    my_list = ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
    res = [item for item in my_list if len(item.split(' ')) < 3]
    

    你自己试过什么了吗?请分享你的研究和尝试,这样你就不会得到你已经尝试过的建议,节省我们所有的时间和挫折。纠正它,确实是错误的过滤器。现在应该可以了,但最好是使用列表理解。
    my_list = ['dirty room', 'do to i', 'dormitory', 'dry to or i', 'i to rod', 'or to i dry', 'rod to i', 'room i dry', 'root i dry', 'to rod i']
    res = [item for item in my_list if len(item.split(' ')) < 3]
    
    >>>res
    ['dirty room', 'dormitory']