如果字符串的一部分重复,则从列表中删除元素

时间:2016-06-23 01:52:52

标签: python string list

我想要做的是从列表中删除包含一些重复部分的字符串元素。例如,如果我已经给出了清单。

const CGFloat kJSQMessagesCollectionViewCellLabelHeightDefault = 20.0f;
const CGFloat kJSQMessagesCollectionViewAvatarSizeDefault = 30.0f; 

我希望输出为

ls = ['02/27/1960', '07/21/2004', '08/13/2004', '09/12/2004', '02/27', '07/21', '08/13']

ls_out = ['02/27/1960', '07/21/2004', '08/13/2004', '09/12/2004'] 已经存在'02/27'

注意,我不确定此问题是否重复)

3 个答案:

答案 0 :(得分:3)

使用for循环和any内置方法也可以解决此问题:

>>> ls
['02/27/1960', '07/21/2004', '08/13/2004', '09/12/2004', '02/27', '07/21', '08/13']
>>>
>>> ls_out = []
>>> 
>>> for x in ls:
        if not any([x in item for item in ls_out]):
            ls_out.append(x)


>>> ls_out
['02/27/1960', '07/21/2004', '08/13/2004', '09/12/2004']

OR:

>>> for x in ls:
        if all([x not in item for item in ls_out]):
            ls_out.append(x)


>>> ls_out
['02/27/1960', '07/21/2004', '08/13/2004', '09/12/2004']

答案 1 :(得分:1)

我不确定这是否是最有效的方法,但它确实有效:

ls = ['02/27/1960', '07/21/2004', '08/13/2004', '09/12/2004', '02/27', '07/21', '08/13']

ls2 = ls

for item in ls:
  for dup_item in ls2:
    if item == dup_item:
      continue
    if item.startswith(dup_item):
      _ = ls.pop(ls.index(dup_item))

print ls

基本上,它会创建两个相同的列表,循环遍历两者并检查它们是否相等 - 如果是,则跳过。如果不是,则检查它们是否从另一个开始。如果是,则将其删除。

答案 2 :(得分:1)

cache = set()
def fun(s):
    ss = s.split('/')
    key = ss[0] + '/' + ss[1]
    if key in cache:
        return None
    else:
        cache.add(key)
        return s

ls = ['02/27/1960', '07/21/2004', '08/13/2004', '09/12/2004', '02/27', '07/21', '08/13']

new_ls = filter(fun, ls)
print new_ls