网络编程
位置:首页>> 网络编程>> Python编程>> Python-apply(lambda x: )的使用及说明

Python-apply(lambda x: )的使用及说明

作者:程序媛小姑娘  发布时间:2022-05-11 04:31:29 

标签:Python,apply,lambda,x:

Python-apply(lambda x: )使用

def instant_order_deal(plat, special_product, clearance_goods, new_product_instant,orders):
   """
   :param plat: 要计算的平台
   :param special_product: 特定库龄产品,其他平台的,amazon的在下面单独读取
   :param clearance_goods: 清仓产品
   :param new_product: 新品
   :param orders: 订单
   :return:
   """
   # 退款订单处理
   orders['订单总金额(包含客户运费、平台补贴)'] = orders.apply(lambda x: 0 if (x['订单类型'] == 'refund') else x['订单总金额(包含客户运费、平台补贴)'], axis=1)
   "中间特定sku处理毛利"
   # orders['毛利'] = orders.apply(lambda x: (x['平均采购价']* 0.4 + x['毛利']) if (x['产品代码'] == 'S4338867210')| (x['产品代码']=='S2130010010') else x['毛利'],axis=1)
   orders['毛利'] = orders.apply(lambda x: (x['毛利'] + 5) if (x['产品代码'] == 'S1416028410') | (x['产品代码'] == 'S1416028440') | (x['产品代码'] == 'S1416028470')  else x['毛利'], axis=1)
   """折价商品毛利计算 + 额温枪"""
   depreciate = read_data().read_depreciate()
   orders['毛利'] = orders.apply(lambda x: (x['平均采购价'] * 0.4 * x['数量']  + x['毛利']) if (x['产品代码'] in depreciate) and x['订单类型'] == 'sale' else x['毛利'],axis=1)

orders['平均采购价'] = orders.apply(lambda x: 0 if (x['订单类型'] == 'resend') else x['平均采购价'], axis=1)
   # 中英仓处理
   orders['仓库分类'] = orders.apply(lambda x: '中仓' if (x['发运仓库'] =='SH [上海奉贤仓]') | (x['发运仓库'] =='WZC [温州仓]') | (x['发运仓库'] =='SZC [深圳仓]') else '海外仓', axis=1)
   # 处理新品
   # if plat == 'ebay' or plat == 'shopee' or plat == 'amazon' :
   newproduct = read_data().read_newproduct()
   orders['仓库分类'] = orders.apply(lambda x: '新品' if (x['产品代码'] in newproduct) else x['仓库分类'], axis=1)

#处理海运产品
   shipping = read_data().read_shipping()
   orders['仓库分类'] =orders.apply(lambda x: '海运产品' if(x['产品代码'] in shipping  and  x['仓库分类'] != '海外仓') else x['仓库分类'],axis=1)

# 当月转清仓处理
   orders['仓库分类'] = orders.apply(lambda x: '特定库龄'if isClearance(x['付款时间'], x['产品代码'], clearance_goods) != None  else x['仓库分类'], axis=1)

# 特定库龄处理
   orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if (x['发运仓库'] == 'GSE [古斯美东仓]' and x['平台']!='ebay') else x['仓库分类'], axis=1)
   if plat == 'amazon':
       # amazon的特定库龄需要单独读取
       special_product_a = read_data().read_special_product(plat)
       special_product_as = read_data().read_special_product('amazon特殊')

orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if (x['产品代码'] in special_product_as) else x['仓库分类'], axis=1)
       orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if ((x['发运仓库'] + x['产品代码']) in special_product_a) else x['仓库分类'], axis=1)

else:
       special_product = read_data().read_special_product('其他平台')
       orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if (x['产品代码'] in special_product) else x['仓库分类'], axis=1)
       orders['仓库分类']=orders.apply(lambda x:'稳定期' if (x['仓库分类']=='中仓')| (x['仓库分类']=='海外仓' )else x['仓库分类'],axis=1 )
   # 处理好仓库分类,接下来判断是否是开发新品
   orders = pd.merge(orders, new_product_instant, on='产品代码', how='left')
   orders['开发新品'] = orders['开发新品'].fillna('非开发新品')
   # 然后处理货值
   orders['货值'] = orders['数量'] * orders['平均采购价']

# orders = pd.merge(orders,mask_instant, on='产品代码', how='left')
   # orders['口罩'] = orders['口罩'].fillna('非口罩')

return orders

python的lambda函数

Lambda 表达式

匿名函数的定义

在 Python 里有两类函数:

  • 第一类:用 def 关键词定义的正规函数

  • 第二类:用 lambda 关键词定义的匿名函数

Python 使用 lambda 关键词来创建匿名函数,而非def关键词,它没有函数名,其语法结构如下:

lambda argument_list: expression
  • lambda - 定义匿名函数的关键词。

  • argument_list - 函数参数,它们可以是位置参数、默认参数、关键字参数,和正规函数里的参数类型一样。

  • :- 冒号,在函数参数和表达式中间要加个冒号。

  • expression - 只是一个表达式,输入函数参数,输出一些值。

注意:

  • expression 中没有 return 语句,因为 lambda 不需要它来返回,表达式本身结果就是返回值。

  • 匿名函数拥有自己的命名空间,且不能访问自己参数列表之外或全局命名空间里的参数。

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

匿名函数的应用

函数式编程 是指代码中每一块都是不可变的,都由纯函数的形式组成。这里的纯函数,是指函数本身相互独立、互不影响,对于相同的输入,总会有相同的输出,没有任何副作用。

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:
 
        y.append(item + 10)
 
    return y
 
x = [1, 2, 3]
 
f(x)
 
print(x)
 
# [1, 2, 3]

匿名函数 常常应用于函数式编程的高阶函数 (high-order function)中,主要有两种形式:

  • 参数是函数 (filter, map)

  • 返回值是函数 (closure)

如,在 filter和map函数中的应用:

filter(function, iterable) 过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 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) 根据提供的函数对指定序列做映射。

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]

除了 Python 这些内置函数,我们也可以自己定义高阶函数。

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

来源:https://blog.csdn.net/TK_Phoenix/article/details/107710803

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com