任何有效的“剥离”项目列表的方法?

时间:2017-03-22 03:30:45

标签: python list

通过“strip”我不是指每个项目的条带,我的意思是删除项目,例如,

a = [None, "a", "b", "c", None, None, None]
b = ["4", "4", "a", "b", "c", "4", "4"]
c = ["4", "4", "a", "4", "b", "c", "4", "4"]

我想保留的是

["a", "b", "c"] # for the first two
["a", "4", "b", "c"] # for the last one

通过这样的方法或功能,

a.strip(None)  # or strip(a, None)   
b.strip("4")  # or strip(b, "4")

我想我可以使用带有标志的for循环来检测两端,但我认为它可能不是pythonic。

6 个答案:

答案 0 :(得分:2)

不,没有内置功能。但是有大约一百万种不同的方法来处理它。我建议在原始列表中使用itertools.dropwhile,反转结果,然后再次执行:

# import the dropwhile function from the itertools module
from itertools import dropwhile

# create a function for this operation
def striplist(l, rem):

        # first, iterate over the list forwards, removing leading occurrences
        # dropwhile returns an iterator, so convert the result to a list
        l = list(dropwhile(lambda x: x == rem, l))
        # then reverse it and do it again to strip the trailing occurrences
        l = list(dropwhile(lambda x: x == rem, l[::-1]))

        return l[::-1] # return the result back in the original order

a = [None, "a", "b", "c", None, None, None]
b = ["4", "4", "a", "b", "c", "4", "4"]
c = ["4", "4", "a", "4", "b", "c", "4", "4"]

print(striplist(a, None))
print(striplist(b, "4"))
print(striplist(c, "4"))

结果:

  

[' a',' b',' c']
  [' a',' b',' c']
  [' a',' 4',''' c']

答案 1 :(得分:0)

列表理解会更加快捷方便 由于它的一行不需要具有单独的功能

a =  [elem for elem in a if elem != None]
b =  [elem for elem in b if elem != '4']

虽然如果你坚持使用一个函数,你可以简单地将它包装在函数中

def strip(l, item):
  return [i for i in l if i != item]

答案 2 :(得分:0)

您可以使用itertools.dropwhile生成一个新列表,其中包含已删除的项目。然后您可以循环,而列表中的最后一项是您要删除并删除它:

from itertools import dropwhile

def lstrip(l, item=None):
    l = list(dropwhile(lambda x: x == item, l))
    while l and l[-1] == item:
        del l[-1]

    return l

a = [None, "a", "b", "c", None, None, None]
b = ["4", "4", "a", "b", "c", "4", "4"]
c = ["4", "4", "a", "4", "b", "c", "4", "4"]

print(lstrip(a))
print(lstrip(b, '4'))
print(lstrip(c, '4'))

输出:

['a', 'b', 'c']
['a', 'b', 'c']
['a', '4', 'b', 'c']

以上将在 O(n)时间内运行。如果您不关心性能,可以将dropwhile替换为另一个将从头开始删除项目的while循环。

答案 3 :(得分:0)

此代码使用列表的pop()方法从列表的开头和结尾删除与item匹配的任何内容。

a = [None, "a", "b", "c", None, None, None]
b1 = ["4", "4", "a", "b", "c", "4", "4"]
b2 = ["4", "4", "a", "4", "b", "c", "4", "4"]

def strip(l, item=None):
    while l[0] == item:
        l.pop(0)
    while l[-1] == item:
        l.pop()
    return l

print(strip(a))
print(strip(b1,'4'))
print(strip(b2,'4'))

运行它会产生请求的输出:

['a', 'b', 'c']
['a', 'b', 'c']
['a', '4', 'b', 'c']

答案 4 :(得分:0)

假设您想要条带函数的行为,但是对于列表,我建议使用以下解决方案:

from itertools import dropwhile, takewhile

def strip(item, lst):
  tmp = list(dropwhile(lambda el: el == item, lst))
  tmp =  list(dropwhile(lambda el: el == item, tmp[::-1]))
  return tmp

这首先从item左侧侧剥离lst的每一次出现。然后我们翻转列表,通过这样做,从列表的右侧侧剥离item的每一次出现。我们最终将列表再次翻转到其原始排序并返回列表。

答案 5 :(得分:-2)

你需要做这样的事情:

list=['a',4,4,4]
def strip(list,no):
    while (1):
        try:
            del list[list.index(no)]
        except:
            break
    return list
print strip(list,4)