利用Python高效處理和排序大型商品數據集
本文演示如何將一個包含商品信息的列表高效地轉換為另一個包含商品統計信息並按特定規則排序的列表。原始數據包含商品標題、顏色、尺碼和數量,目標是統計相同標題、顏色和尺碼的商品數量,併計算每個標題下所有商品的總數量,最終結果按總數量降序排列。
示例原始數據:
data = [ ('連衣裙', '白色', 'S', 1), ('連衣裙', '白色', 'S', 1), ('連衣裙', '黑色', 'M', 1), ('褲子', '白色', 'S', 1), ]
目標輸出格式:
result = [ ('連衣裙', '白色', 'S', 2, 3), ('連衣裙', '黑色', 'M', 1, 3), ('褲子', '白色', 'S', 1, 1), ]
其中,最後一個數字代表該標題下所有商品的總數量。為了高效處理大型數據集,我們將使用Python的collections.Counter
對象進行計數。
以下代碼實現了這一功能:
from collections import Counter data = [ ('連衣裙', '白色', 'S', 1), ('連衣裙', '白色', 'S', 1), ('連衣裙', '黑色', 'M', 1), ('褲子', '白色', 'S', 1), ] # 使用Counter統計每個(標題, 顏色, 尺碼)組合的數量item_counts = Counter(tuple(item[:-1]) for item in data) # 使用Counter統計每個標題的總數量title_totals = Counter(item[0] for item in data) # 創建結果列表,包含標題、顏色、尺碼、數量和標題總數量result = [ (*item, count, title_totals[item[0]]) for item, count in item_counts.items() ] # 按標題總數量降序排序,然後按標題、顏色、尺碼升序排序result.sort(key=lambda x: (-x[4], x[0], x[1], x[2])) print(result)
這段代碼首先利用Counter
高效地統計每個商品組合和每個標題的出現次數。然後,它構造一個新的列表,包含所有必要的信息,並根據總數量進行排序。這種方法充分利用了Counter
的效率,能夠輕鬆處理包含數千條甚至更多記錄的大型數據集。
以上是如何用Python高效統計並排序大型商品數據集?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

WebStorm Mac版
好用的JavaScript開發工具