我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用builtins.filter()。
def oldfilter(*args): """ filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list. """ mytype = type(args[1]) if isinstance(args[1], basestring): return mytype().join(builtins.filter(*args)) elif isinstance(args[1], (tuple, list)): return mytype(builtins.filter(*args)) else: # Fall back to list. Is this the right thing to do? return list(builtins.filter(*args)) # This is surprisingly difficult to get right. For example, the # solutions here fail with the test cases in the docstring below: # http://stackoverflow.com/questions/8072755/
def __and__(self, other): ''' Intersection is the minimum of corresponding counts. >>> Counter('abbb') & Counter('bcc') Counter({'b': 1}) ''' if not isinstance(other, Counter): return NotImplemented _min = min result = Counter() if len(self) < len(other): self, other = other, self for elem in filter(self.__contains__, other): newcount = _min(self[elem], other[elem]) if newcount > 0: result[elem] = newcount return result
def compute_common_numeric_tokens(column, feature, k): """ compute top k frequent numerical tokens and their counts. tokens are integer or floats e.g. "123", "12.3" """ col = column.str.split(expand=True).unstack().dropna().values token = np.array(list(filter(lambda x: hf.is_Decimal_Number(x), col))) if token.size: feature["frequent-entries"]["most_common_numeric_tokens"] = ordered_dict2(token, k)
def compute_common_alphanumeric_tokens(column, feature, k): """ compute top k frequent alphanumerical tokens and their counts. tokens only contain alphabets and/or numbers, decimals with points not included """ col = column.str.split(expand=True).unstack().dropna().values token = np.array(list(filter(lambda x: x.isalnum(), col))) if token.size: feature["frequent-entries"]["most_common_alphanumeric_tokens"] = ordered_dict2(token, k)
def gen_conv_testcase(): """ Generate convolution test cases Returns: Iterator of (C, D, H, W, N, T, R, S, K, strides, padding) """ def args_filter(args): """ Filter currently not supported case (only symmetric padding allowed). Returns: True if args shall be kept. """ C, D, H, W, N, T, R, S, K, strides, padding = args pad_t, pad_b, pad_l, pad_r = common_conv2d_pool_padding((N, H, W, C), (R, S, C, K), strides, padding) return pad_t == pad_b and pad_l == pad_r # test params Cs = [2, ] Ds = [1, ] Hs = [28, 11] Ws = [28, 11] Ns = [8, ] Ts = [1, ] Rs = [1, 3] Ss = [1, 3] Ks = [4, ] strides_list = [[1, 1, 1, 1], [1, 2, 3, 1]] paddings = ['SAME', 'VALID'] all_args = list( itertools.product(Cs, Ds, Hs, Ws, Ns, Ts, Rs, Ss, Ks, strides_list, paddings)) return filter(args_filter, all_args)
def gen_pool_testcase(): """ Generate pooling test cases Returns: Iterator of (C, D, H, W, N, J, T, R, S, strides, padding) """ def args_filter(args): """ Filter currently not supported case (only symmetric padding allowed). Returns: True if args shall be kept. """ C, D, H, W, N, J, T, R, S, strides, padding = args pad_t, pad_b, pad_l, pad_r = common_conv2d_pool_padding((N, H, W, C), (R, S, C, C), strides, padding) return pad_t == pad_b and pad_l == pad_r # test params Cs = [2, ] Ds = [1, ] Hs = [28, 11] Ws = [28, 11] Ns = [8, ] Js = [1, ] Ts = [1, ] Rs = [1, 3] Ss = [1, 3] strides_list = [[1, 1, 1, 1], [1, 2, 3, 1]] paddings = ['SAME', 'VALID'] all_args = list( itertools.product(Cs, Ds, Hs, Ws, Ns, Js, Ts, Rs, Ss, strides_list, paddings)) return filter(args_filter, all_args)
def filter(*args, **kwargs): return list(builtins.filter(*args, **kwargs))
def lfilter(*args, **kwargs): return list(filter(*args, **kwargs))
def test_import_builtins(self): before = """ a = raw_input() b = open(a, b, c) c = filter(a, b) d = map(a, b) e = isinstance(a, str) f = bytes(a, encoding='utf-8') for g in xrange(10**10): pass h = reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) super(MyClass, self) """ after = """ from builtins import bytes from builtins import filter from builtins import input from builtins import map from builtins import range from functools import reduce a = input() b = open(a, b, c) c = list(filter(a, b)) d = list(map(a, b)) e = isinstance(a, str) f = bytes(a, encoding='utf-8') for g in range(10**10): pass h = reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) super(MyClass, self) """ self.convert_check(before, after, ignore_imports=False, run=False)
def filter(function, iterable): """Replacement for the built-in :func:`filter() <python:filter>` function.""" return builtins.filter(function, iterable)