用python模仿c#的List<>一组list筛选扩展方法

说明:有些地方找不到就抛错了,实际上不算合理,请自行斟酌!

另外可以举一反三,扩展很多小功能。

很多小功能,能再一定程度提高开发效率

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by weilai on 2016/12/28



# 查找单个 lambda 表达式真的单个结果
def find(data_list, find_lambda):
    if not isinstance(data_list, list):
        return
    for item in data_list:
        if find_lambda(item):
            return item
    return None

# 首个,没有就报错
def first(data_list, find_lambda):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    for item in data_list:
        if find_lambda(item):
            return item
    raise Exception('not find')

# 查找全部
def find_all(data_list, find_lambda):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    temp_list = []
    for item in data_list:
        if find_lambda(item):
            temp_list.append(item)
    return temp_list

# 类似c#的select方法
def select(data_list, select_lambda):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    temp_list = []
    for item in data_list:
        field = select_lambda(item)
        temp_list.append(field)
    return temp_list

#
def select_many(data_list, select_lambda):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    temp_list = []
    for item in data_list:
        fields = select_lambda(item)
        temp_list.append(fields)
    return temp_list

# 统计和
def sum(data_list, sum_lambda):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    temp = None
    for item in data_list:
        field = sum_lambda(item)
        temp += field
    return temp

# 最大值
def max(data_list, func):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    if len(data_list) <= 0:
        raise Exception('list no item')
    data_list.sort(key=func)
    temp = data_list[-1]
    return temp

# 最小值
def min(data_list, func):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    if len(data_list) <= 0:
        raise Exception('list no item')
    data_list.sort(key=func)
    temp = data_list[0]
    return temp

# 是否存在
def exists(data_list, equals_lambda):
    if not isinstance(data_list, list):
        raise Exception('item type not equals')
    for item in data_list:
        if equals_lambda(item):
            return True
    return False

# 相等,这个 很可能不严密。谨慎使用
def equals(item1, item2):
    if item1 == item2:
        return True
    return False

# 好像是提取不同
def distinct(data_list, equals_lambda):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    temp = []
    for x in data_list:
        has = False
        for y in temp:
            if equals_lambda(x, y):
                has = True
                break
        if not has:
            temp.append(x)
    return temp

# 分组
def group_by(data_list, item_lambda):
    if not isinstance(data_list, list):
        raise Exception('it is not list')
    temp = []
    fin_temp = []
    for x in data_list:
        has = False
        for y in temp:
            if y == item_lambda(x):
                has = True
                break
        if not has:
            temp.append(item_lambda(x))
    for t in temp:
       fin_temp.append((t, find_all(data_list, lambda m: item_lambda(m) == t)))
    return fin_temp

# 测试
L = [('b', 1), ('a', 2), ('c', 4), ('c', 3)]
L.sort(key=lambda x: x[0])
print(find_all(L, lambda x:x[0] == 'a'))
print(distinct(L, lambda x, y: x[0] == y[0]))
print(group_by(L, lambda x: x[0]))
print(L)

 

posted on 2017-09-26 14:38  未来无限  阅读(530)  评论(0编辑  收藏  举报

导航