tags = [''tag1'',''+'',''tag2'',''+'',''tag3'','' - '','''tag4''] include,exclude = [],[] op =''+'' for cur in tags: 如果是'+ - ''': op = cur else: 如果op ==''+' ': include.append(cur) else: exclude.append(cur) --Bentags = [''tag1'', ''+'', ''tag2'', ''+'', ''tag3'', ''-'', ''tag4'']include, exclude = [], []op = ''+''for cur in tags:if cur in ''+-'':op = curelse:if op == ''+'':include.append(cur)else:exclude.append(cur)--Ben = [''+'',''tag1'',''+'',''tag2'','' - '',''tag3'',''+'',''tag4''] 导入itertools b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1) ,8)) 结果1 = [x [1] for x in itertools.izip(b,c)if x [0] ==''+''] result2 = [x [1] for x in itertools.izip(b,c)if x [0] =='' - ''] 打印打印结果1 打印结果2 Gerard a = [ ''+'', ''tag1'', ''+'', ''tag2'', ''-'', ''tag3'', ''+'', ''tag4'' ] import itertools b = list(itertools.islice(a,0,8,2)) c = list(itertools.islice(a,1,8,2)) result1 = [x[1] for x in itertools.izip(b,c) if x[0] == ''+''] result2 = [x[1] for x in itertools.izip(b,c) if x[0] == ''-''] print print result1 print result2 Gerard ''8''是''''的长度(len(a))''8'' is the length of ''a'' (len(a)) 这篇关于根据前面的元素从列表中分离元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 19:36