リストの要素ごとの追加: Python 的アプローチ
2 つのリストの要素ごとの追加は、いくつかの組み込みメソッドを使用して Python で簡単に実行できます。関数で。面倒な反復を行わずにこれを実現する方法は次のとおりです。
operator.add で map() を使用する:
from operator import add result = list(map(add, list1, list2))
map() 関数は、add 関数をそれぞれに適用します。 list1 と list2 の対応する要素を検索し、結果のリストを返します。
または、次のように使用します。 リスト内包表記を使用した zip():
result = [sum(x) for x in zip(list1, list2)]
zip() 関数は、list1 と list2 の要素をペアにして一連のタプルにします。次に、リスト内包表記によって各タプルの合計が計算され、要素ごとの加算が生成されます。
パフォーマンスの比較:
これらのアプローチの効率を比較するために、タイミングを実行しました。大きなリスト (100,000 要素) のテスト:
>>> from itertools import izip >>> list2 = [4, 5, 6] * 10 ** 5 >>> list1 = [1, 2, 3] * 10 ** 5 >>> %timeit from operator import add; map(add, list1, list2) 10 loops, best of 3: 44.6 ms per loop >>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)] 10 loops, best of 3: 71 ms per loop >>> %timeit [a + b for a, b in zip(list1, list2)] 10 loops, best of 3: 112 ms per loop >>> %timeit from itertools import izip; [sum(x) for x in izip(list1, list2)] 1 loops, best of 3: 139 ms per loop >>> %timeit [sum(x) for x in zip(list1, list2)] 1 loops, best of 3: 177 ms per loop
次のように結果が示すように、operator.add を使用した map() アプローチは、大きなリストに対しては最も高速です。
以上がPython でリストの要素ごとの追加を実行する最も効率的な方法は何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SublimeText3 中国語版
中国語版、とても使いやすい

メモ帳++7.3.1
使いやすく無料のコードエディター

Dreamweaver Mac版
ビジュアル Web 開発ツール
