Python の sort() メソッドは、配列の並べ替えに使用されます。この記事では、例の形式でこれについて詳しく説明します。
1.基本形
リストには、リストをその場でソートする独自のソートメソッドがあります。これはインプレースソートであるため、タプルは変更できないため、このメソッドを持てないことは明らかです。
x = [4, 6, 2, 1, 7, 9] x.sort() print x # [1, 2, 4, 6, 7, 9]元のリストを変更せずにソートされたコピーが必要な場合、それを実現する方法
x =[4, 6, 2, 1, 7, 9] y = x[ : ] y.sort() print y #[1, 2, 4, 6, 7, 9] print x #[4, 6, 2, 1, 7, 9]注:
y = x[:] は、シャーディング操作を通じてリスト x のすべての要素を y にコピーします。単に x を y に代入する場合: y = x、y および x は同じリストを指します。コピーが生成されます 。
ソートされたリストのコピーを取得する別の方法は、sorted 関数を使用することです。
x =[4, 6, 2, 1, 7, 9] y = sorted(x) print y #[1, 2, 4, 6, 7, 9] print x #[4, 6, 2, 1, 7, 9]sorted は順序付けされたコピーを返します。タイプは次のように常にリストです。
print sorted('Python') #['P', 'h', 'n', 'o', 't', 'y']
2. カスタム比較関数
独自の比較関数を定義し、パラメータを通じて並べ替えメソッドに渡すことができます。
def comp(x, y): if x < y: return 1 elif x > y: return -1 else: return 0 nums = [3, 2, 8 ,0 , 1] nums.sort(comp) print nums # 降序排序[8, 3, 2, 1, 0] nums.sort(cmp) # 调用内建函数cmp ,升序排序 print nums # 降序排序[0, 1, 2, 3, 8]
3. オプションのパラメータ
sort メソッドには、key と reverse という 2 つのオプションのパラメータもあります
1. キーを使用する場合は、並べ替えプロセスによって呼び出される関数を提供する必要があります:
x = ['mmm', 'mm', 'mm', 'm' ] x.sort(key = len) print x # ['m', 'mm', 'mm', 'mmm']2. Reverse は降順ソートを実装し、ブール値を指定する必要があります:
y = [3, 2, 8 ,0 , 1] y.sort(reverse = True) print y #[8, 3, 2, 1, 0]

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 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

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

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

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

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

ホットトピック









