有人说
filter
?
selected_tokens = list(filter(lambda x: len(x) > 5 and c[x] > 100, token_list))
此外,您可以使用
c[...]
如果你想要速度,可以使用列表理解:
selected_tokens = [x for x in token_list if len(x) > 5 and c[x] > 100]
如果你想获得满足你条件的单词而没有多余的重复,请使用
set
token_set = set(token_list)
selected_tokens = [x for x in token_set if if len(x) > 5 and c[x] > 100]
当心,秩序已失。如果您希望订单没有重复项,请使用
OrderedDict
(python<3.6或
dict
(python>=3.6)。
dict_ = OrderedDict()
for t in token_list:
dict_[t] = None
selected_tokens = [x for x in dict_ if len(x) > 5 and c[x] > 100]
如果a
字典
如果不这样做,你可以看看
OrderedSet
制作并实施具有相同效果的东西:
token_set = OrderedSet(token_list)
selected_tokens = [x for x in token_set if ...] # as usual