1、过滤问题
找出以ip开头的字符串 _input = ['ip_10.2.223.5', 'ip_10.2.220.3', 'port_22', 'port_8001', 'port_80', 'ip_172.16.8.35'] for item in _input: if item.startswith('ip'): print(item)
内建函数filter(fucn,seq)
python2:中,返回结果是一个列表
python3中:返回filter对象
def get_int(n): if isinstance(n,int): return True else: return False _input = [1, 2, 3, "ss", "test", [1, 2]] ret = [] for item in filter(get_int,_input): ret.append(item) print(ret)
统计列表中每个字符出现的次数
mylist = ['abc123','bc','cd2',['bc32'],'13',['ab1'],'12'] mysum = {} def mycount(seq): for item in seq: if type(item) == list: mycount(item) #如果是列表,就递归调用函数 else: for i in item: if i in mysum: mysum[i] += 1 else: mysum[i] = 1 mycount(mylist) for key in mysum: print(key,':',mysum[key])