['and', 'B', ['not', 'A'],[1,2,1,[2,1],[1,1,[2,2,1]]], ['not', 'A', 'A'],['or', 'A', 'B' ,'A'] , 'B']
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def unilist(ll):
"""
功能:用递归方法删除多层列表中重复元素
"""
result = []
for i in ll:
if isinstance(i, list):
if unilist(i) not in result:
result.append(unilist(i))
else:
if i not in result:
result.append(i)
return result
def flatten(ll):
"""
功能:用递归方法展开多层列表,以生成器方式输出
"""
if isinstance(ll, list):
for i in ll:
for element in flatten(i):
yield element
else:
yield ll
testcase= ['and', 'B', ['not', 'A'],[1,2,1,[2,1],[1,1,[2,2,1]]], ['not', 'A', 'A'],['or', 'A', 'B' ,'A'] , 'B']
print unilist(testcase)
print list(flatten(testcase))
['and', 'B', ['not', 'A'], [1, 2, [2, 1], [1, [2, 1]]], ['or', 'A', 'B']] ['and', 'B', 'not', 'A', 1, 2, 1, 2, 1, 1, 1, 2, 2, 1, 'not', 'A', 'A', 'or', 'A', 'B', 'A', 'B']
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有