網站首頁 編程語言 正文
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
相關推薦
- 2022-07-13 Python中常用序列數據結構
- 2022-06-28 python神經網絡tfrecords文件的寫入讀取及內容解析_python
- 2022-04-15 windows+vscode穿越跳板機調試遠程代碼的圖文教程_python
- 2022-01-10 修改代碼后,刷新頁面沒有更新的解決辦法。Disable cache禁止
- 2022-09-03 列表頁常見hook封裝實例_React
- 2023-02-12 JetpackCompose?Navigation導航實現流程_Android
- 2022-12-22 C語言如何使用函數求素數和舉例_C 語言
- 2022-05-14 react中的axios模塊你了解嗎_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支