【python cookbook】【数据结构与算法】16.筛选序列中的元素

问题:提取出序列中的值或者根据某些标准对序列做删减

解决方案:列表推导式、生成器表达式、使用内建的filter()函数

1、列表推导式方法:存在一个潜在的缺点,如果输入数据非常大可能会产生一个庞大的结果,考虑到该问题,建议选择生成器表达式

# Examples of different ways to filter data

mylist = [1, 4, -5, 10, -7, 2, 3, -1]
print('mylist=',mylist)


# 使用列表推导式
pos = [n for n in mylist if n > 0]
print('正数为:',pos)

neg = [n for n in mylist if n < 0]
print('负数为:',neg)
>>> ================================ RESTART ================================
>>> 
mylist= [1, 4, -5, 10, -7, 2, 3, -1]
正数为: [1, 4, 10, 2, 3]
负数为: [-5, -7, -1]
>>> 

2、生成器表达式方法:

mylist = [1, 4, -5, 10, -7, 2, 3, -1]
print('mylist=',mylist)

# 使用生成器表达式
pos = (n for n in mylist if n > 0)
print('生成器为',pos)

for x in pos:
    print(x)
>>> ================================ RESTART ================================
>>> 
mylist= [1, 4, -5, 10, -7, 2, 3, -1]
生成器为 <generator object <genexpr> at 0x02421FD0>
1
4
10
2
3

3、如果筛选标准无法简单表示在列表推导式或者生成器表达式中,比如筛选过程涉及一些异常处理或者更复杂的细节,可以考虑将处理筛选逻辑的代码放到单独的函数中,然后使用内建的filter()函数处理。

values=['1','2','-3','-','N/A','4','5','%']

def is_int(val):  #将处理筛选逻辑的代码放到单独的函数
    try:
        x=int(val)
        return True
    except ValueError:
        return False

ivals=list(filter(is_int,values)) #使用filter(func,list)进行过滤
print(ivals)
>>> ================================ RESTART ================================
>>> 
['1', '2', '-3', '4', '5']
>>> 

filter(func,list)会创建一个迭代器,如果想要列表形式的结果,需使用list()将结果转为列表。

补充:

用新值替换掉不满足标准的值,而不是丢弃它们,可通过将筛选条件移到一个条件表达式中来轻松实现。

# Negative values clipped to 0
neg_clip = [n if n > 0 else 0 for n in mylist]
print('负数替换为0,结果:',neg_clip)

# Positive values clipped to 0
pos_clip = [n if n < 0 else 0 for n in mylist]
print('正数替换为0,结果:',pos_clip)
'''
>>> ================================ RESTART ================================
>>> 
mylist= [1, 4, -5, 10, -7, 2, 3, -1]
负数替换为0,结果: [1, 4, 0, 10, 0, 2, 3, 0]
正数替换为0,结果: [0, 0, -5, 0, -7, 0, 0, -1]
>>> 

值得推荐的工具itertools.compress(),它接受一个可迭代对象以及一个布尔选择序列作为输入。

如果想把对一个序列的筛选结果施加到另一个相关的序列上时,就会非常有用。

# 采用筛选工具itertools.compress()

addresses = [
    '5412 N CLARK',
    '5148 N CLARK', 
    '5800 E 58TH',
    '2122 N CLARK',
    '5645 N RAVENSWOOD',
    '1060 W ADDISON',
    '4801 N BROADWAY',
    '1039 W GRANVILLE',
]

counts = [ 0, 3, 10, 4, 1, 7, 6, 1]

from itertools import compress

more5 = [ n > 5 for n in counts ]
a = list(compress(addresses, more5))
print(a)
>>> ================================ RESTART ================================
>>> 
['5800 E 58TH', '1060 W ADDISON', '4801 N BROADWAY']
>>> 

这里的关键是首先创建一个布尔序列,用来表示哪个元素可满足我们的条件。然后compress()函数挑选出满足布尔值为True的相应元素。

同filter()函数一样,正常情况下compress()函数返回一个迭代器,若需要返回列表则需使用list()将结果转为列表。

原文地址:https://www.cnblogs.com/apple2016/p/5747529.html