itertools模塊 chain遍歷多個可迭代對象

from itertools import chain

chain(*iterables)
將多個可迭代對象進行合併,相當於如下代碼:

def chain(*iterables):
    # chain('XYZ', 'DEF') --> X Y Z D E F
    for it in iterables:
        for element in it:
            yield element

############ ex.01
ops={}
ops['missing'] = ['gx_mean','gy_mean']
ops['collinear'] = ['gx_skew','gy_skew',]
ops['zero_importance'] = ['gx_kurt','gy_kurt']
ops['low_importance'] = ['gx_range','gy_range']
from itertools import chain
all_identified = set(list(chain(*list(ops.values()))))
all_identified
Out[112]: 
{'gx_kurt',
 'gx_mean',
 'gx_range',
 'gx_skew',
 'gy_kurt',
 'gy_mean',
 'gy_range',
 'gy_skew'}

 

ops['missing'] = ['gx_mean','gy_mean','gz_mean','ax_mean','ay_mean','az_mean',
                     'gx_std','gy_std','gz_std','ax_std','ay_std','az_std']

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章