Use python to simulate c # List & lt; & gt; A set of list filtering extension methods,

Source: Internet
Author: User

Use python to simulate c # List <> A set of list filtering extension methods,

Note: An error is thrown if it is not found in some places. It is not reasonable. please consider it yourself!

In addition, many small functions can be extended.

Many small functions can improve development efficiency to a certain extent

#! /Usr/bin/env python #-*-coding: UTF-8-*-# Created by weilai on 2016/12/28 # find a single lambda expression. True single result 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 # The first one. if not, the following error is reported: 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) # find all 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 # select method def (data_list, select_lambda) similar to c #: 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_struct (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 # statistics and 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 # maximum 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 # minimum 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 # equal, this is probably not strict. Exercise caution when using def equals (item1, item2): if item1 = item2: return True return False # It seems that different 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 s_s_lambda (x, y ): has = True break if not has: temp. append (x) return temp # group 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 # test 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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.