python 字符串的列表 怎么去除具有包含关系的字符串
1个回答
展开全部
# encoding: utf-8
# 一个直接逻辑的笨办法
# encoding: utf-8
clist = ['abcd','bcde','ab','cd','ad']
# 按字符串长度排序
def _cmp(a,b): return len(a)-len(b)
clist = sorted(clist, _cmp)
rs = []
for i,c in enumerate(clist):
for b in clist[i+1:]:
if c in b:
break
else:
rs.append(c)
print rs
#~ >pythonw -u "tryBaidu.py"
#~ ['ad', 'abcd', 'bcde']
#~ >Exit code: 0 Time: 0.594
# encoding: utf-8
clist = ['abcd','bcde','ab','cd','ad']
# 按字符串长度排序
def _cmp(a,b): return len(a)-len(b)
clist = sorted(clist, _cmp)
# 若分隔符"|"不可能出现在任何子串中,则可:
rs = [c for i,c in enumerate(clist)
if c not in '|'.join(clist[i+1:])]
print rs
#~ >pythonw -u "tryBaidu.py"
#~ ['ad', 'abcd', 'bcde']
#~ >Exit code: 0 Time: 0.567
# 一个直接逻辑的笨办法
# encoding: utf-8
clist = ['abcd','bcde','ab','cd','ad']
# 按字符串长度排序
def _cmp(a,b): return len(a)-len(b)
clist = sorted(clist, _cmp)
rs = []
for i,c in enumerate(clist):
for b in clist[i+1:]:
if c in b:
break
else:
rs.append(c)
print rs
#~ >pythonw -u "tryBaidu.py"
#~ ['ad', 'abcd', 'bcde']
#~ >Exit code: 0 Time: 0.594
# encoding: utf-8
clist = ['abcd','bcde','ab','cd','ad']
# 按字符串长度排序
def _cmp(a,b): return len(a)-len(b)
clist = sorted(clist, _cmp)
# 若分隔符"|"不可能出现在任何子串中,则可:
rs = [c for i,c in enumerate(clist)
if c not in '|'.join(clist[i+1:])]
print rs
#~ >pythonw -u "tryBaidu.py"
#~ ['ad', 'abcd', 'bcde']
#~ >Exit code: 0 Time: 0.567
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询