# 举个栗子: delim = u';' L = [u'今天是个好天气!', u'对呀对呀;我也这么想~'] ret = [x for j in L for x in j.split(delim)]
# 其含义是: ret = [] delim = u';' for sentence in L: for items in sentence.split(delim): ret.append(items)
# 应用场景实例如下,需求为将传入句子按分号隔开获得新的句子列表: defsplit_list(L, delim=';'): if L.__len__() == 0: return [] ifisinstance(L[0], unicode) andisinstance(delim, str): delim = delim.decode('utf-8') ret = [x for j in L for x in j.split(delim)] returnfilter(lambda x: x != '', ret)
# 举个栗子: dic = { 1:{'index':2, 'data':'klm'}, 2:{'index':7, 'data':'cde'}, } ran = xrange(1,5) ret = [(k, v) for (k, v) in dic.items() if v['index'] in ran]
# 其含义是: ret = [] for (k,v) in dic.items(): idx = v['index'] if idx in ran: ret.append( (k,v) )
# 应用实例如下,需求为每次生成满足条件的子dict, # 并更新元素中几个字段表示的在新dict中的位置: def dict_select(self, dic, lef, rig=None, pos_lef=None): """ return a dict which satisfied the range of current segment. :param dic: a dict :param lef: left index of word_index :param rig: right index of word_index :param pos_lef: left index of position """ def change_pos(inp): cp = inp.copy() # MUST BE A COPY for no-modify cp[u'word_index'] = inp[u'word_index'] - lef cp[u'position'] = inp[u'position'] - pos_lef return cp
if rig: ran = xrange(lef, rig) ret = [(k, v) for (k, v) in dic.items() if v[u'word_index'] in ran] else: ret = [(k, v) for (k, v) in dic.items() if v[u'word_index'] >= lef] return dict([(k, change_pos(v)) for (k, v) in ret])