SoFunction
Updated on 2024-11-17

Usage and description of Python-apply(lambda x: )

Python-apply(lambda x: ) uses the

def instant_order_deal(plat, special_product, clearance_goods, new_product_instant,orders):
    """
    :param plat: platform to be calculated
    :param special_product: special product, for other platforms, amazon's is read separately below
    :param clearance_goods: Clearance products
    :param new_product: New products
    :param orders: Orders
    :return.
    """
    # Refund order processing
    orders['Total Order Amount(Includes customer shipping costs、Platform subsidies)'] = (lambda x: 0 if (x['Order Type'] == 'refund') else x['Total Order Amount(Includes customer shipping costs、Platform subsidies)'], axis=1)
    "Intermediate Sku-Specific Processing Gross Profit"
    # orders['Gross Profit'] = (lambda x: (x['Average Purchase Price'] * 0.4 + x['Gross Profit']) if (x['Product Code'] == 'S4338867210') | (x['Product Code'] == 'S2130010010') else x['Gross Profit'],axis=1)
    orders['Gross profit'] = (lambda x: (x['Gross profit'] + 5) if (x['Product Code'] == 'S1416028410') | (x['Product Code'] == 'S1416028440') | (x['Product Code'] == 'S1416028470')  else x['Gross profit'], axis=1)
    """Gross margin calculation for discounted goods + forehead warming gun"""
    depreciate = read_data().read_depreciate()
    orders['Gross profit'] = (lambda x: (x['Average purchase price'] * 0.4 * x['Number']  + x['Gross profit']) if (x['Product Code'] in depreciate) and x['Order Type'] == 'sale' else x['Gross profit'],axis=1)


    orders['Average purchase price'] = (lambda x: 0 if (x['Order Type'] == 'resend') else x['Average purchase price'], axis=1)
    # Chinese and English warehouse processing
    orders['Warehouse classification'] = (lambda x: 'Nakakura' if (x['Shipping warehouse'] =='SH [Shanghai Fengxian Warehouse]') | (x['Shipping warehouse'] =='WZC [Wenzhou Warehouse]') | (x['Shipping warehouse'] =='szc [shenzhen warehouse]') else 'Overseas warehouse', axis=1)
    # Handling new products
    # if plat == 'ebay' or plat == 'shopee' or plat == 'amazon' :
    newproduct = read_data().read_newproduct()
    orders['Warehouse classification'] = (lambda x: 'New Products' if (x['Product Code'] in newproduct) else x['Warehouse classification'], axis=1)

    # Handling of marine products
    shipping = read_data().read_shipping()
    orders['Warehouse classification'] =(lambda x: 'Marine products' if(x['Product Code'] in shipping  and  x['Warehouse classification'] != 'Overseas warehouse') else x['Warehouse classification'],axis=1)

    # Current month to be converted to liquidation
    orders['Warehouse classification'] = (lambda x: 'Specific library age'if isClearance(x['Time of payment'], x['Product Code'], clearance_goods) != None  else x['Warehouse classification'], axis=1)

    # Age-specific treatments
    orders['Warehouse classification'] = (lambda x: 'Specific library age' if (x['Shipping warehouse'] == 'GSE [Gusme East Silo]' and x['Platform']!='ebay') else x['Warehouse classification'], axis=1)
    if plat == 'amazon':
        # amazon's specific library age needs to be read separately
        special_product_a = read_data().read_special_product(plat)
        special_product_as = read_data().read_special_product('amazon special')

        orders['Warehouse classification'] = (lambda x: 'Specific library age' if (x['Product Code'] in special_product_as) else x['Warehouse classification'], axis=1)
        orders['Warehouse classification'] = (lambda x: 'Specific library age' if ((x['Shipping warehouse'] + x['Product Code']) in special_product_a) else x['Warehouse classification'], axis=1)

    else:
        special_product = read_data().read_special_product('Other platforms')
        orders['Warehouse classification'] = (lambda x: 'Specific library age' if (x['Product Code'] in special_product) else x['Warehouse classification'], axis=1)
        orders['Warehouse classification']=(lambda x:'Stabilization period' if (x['Warehouse classification']=='Nakakura')| (x['Warehouse classification']=='Overseas warehouse' )else x['Warehouse classification'],axis=1 )
    # Handle the warehouse classification, next determine if it's a new development product #
    orders = (orders, new_product_instant, on='Product Code', how='left')
    orders['Developing new products'] = orders['Developing new products'].fillna('Non-developmental new products')
    # Then process the value
    orders['Value of goods'] = orders['Number'] * orders['Average purchase price']

    # orders = (orders,mask_instant, on='product code', how='left')
    # orders['masks'] = orders['masks'].fillna('non-masks')

    return orders

Python's lambda function

Lambda Expressions

Definition of anonymous functions

There are two types of functions in Python:

  • Class I: Regular functions defined with the def keyword
  • Class 2: Anonymous functions defined with the lambda keyword

Python uses the lambda keyword to create anonymous functions, rather than the def keyword, which has no function name and has the following syntactic structure:

lambda argument_list: expression
  • lambda - Defines the keywords of the anonymous function.
  • argument_list - Function parameters, which can be positional, default, and keyword parameters, are the same types of parameters as in regular functions.
  • :- A colon, which is to be placed between a function parameter and an expression.
  • expression - It's just an expression that takes in a function argument and outputs some value.

Attention:

  • There is no return statement in expression because lambda doesn't need it to return; the result of the expression itself is the return value.
  • Anonymous functions have their own namespace and cannot access arguments outside their own argument list or in the global namespace.
def sqr(x):
 
    return x ** 2
 
print(sqr)
 
# <function sqr at 0x000000BABD3A4400>
 
y = [sqr(x) for x in range(10)]
 
print(y)
 
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
 
lbd_sqr = lambda x: x ** 2
 
print(lbd_sqr)
 
# <function <lambda> at 0x000000BABB6AC1E0>
 
y = [lbd_sqr(x) for x in range(10)]
 
print(y)
 
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
 
 
sumary = lambda arg1, arg2: arg1 + arg2
 
print(sumary(10, 20))  # 30
 
func = lambda *args: sum(args)
 
print(func(1, 2, 3, 4, 5))  # 15

Applications of Anonymous Functions

Functional Programming means that every piece of code is immutable and consists of a pure function. A pure function here means that the functions themselves are independent of each other and do not affect each other; for the same input, there will always be the same output, without any side effects.

def f(x):
 
    for i in range(0, len(x)):
 
        x[i] += 10
 
    return x
 
x = [1, 2, 3]
 
f(x)
 
print(x)
 
# [11, 12, 13]
 
def f(x):
 
    y = []
 
    for item in x:
 
        (item + 10)
 
    return y
 
x = [1, 2, 3]
 
f(x)
 
print(x)
 
# [1, 2, 3]

Anonymous functions are often used in functional programming in high-order functions (high-order function), there are two main forms:

  • Arguments are functions (filter, map)
  • The return value is a function (closure)

e.g., in the filter and map functions:

filter(function, iterable) Filters the sequence, filtering out elements that don't match the conditions and returning an iterator object, which can be converted to a list using list() if you want to convert to a list.

odd = lambda x: x % 2 == 1 
templist = filter(odd, [1, 2, 3, 4, 5, 6, 7, 8, 9]) 
print(list(templist))  # [1, 3, 5, 7, 9]

map(function, *iterables) Maps the specified sequence according to the provided function.

m1 = map(lambda x: x ** 2, [1, 2, 3, 4, 5])
 
print(list(m1))  
 
# [1, 4, 9, 16, 25]
 
m2 = map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
 
print(list(m2))  
 
# [3, 7, 11, 15, 19]

In addition to Python's built-in functions, you can also define your own higher-order functions.

def apply_to_list(fun, some_list):
 
    return fun(some_list)
 
lst = [1, 2, 3, 4, 5]
 
print(apply_to_list(sum, lst))
 
# 15
 
print(apply_to_list(len, lst))
 
# 5
 
print(apply_to_list(lambda x: sum(x) / len(x), lst))
 
# 3.0

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.