オブジェクトのリストのシャッフル
問題:
ランダム.シャッフル()関数。予期されたシャッフル動作は発生しないようで、関数は代わりに None を出力します。
解決策:
オブジェクトのリストを効果的にシャッフルするには、shuffle( ) ランダムモジュールからの関数は次のようになります
from random import shuffle objects = [obj1, obj2, obj3] # initialize the list of objects shuffle(objects) # shuffle the objects in place
説明:
他のプログラミング言語とは異なり、Python では、random.shuffle() はその場で動作します。つまり、元のリストを変更せずに変更します。新しいものを返す。さらに、オブジェクトの変更が意図されている場合、Python の関数は None を返すのが一般的です。
サンプル コード:
from random import shuffle x = [[i] for i in range(10)] shuffle(x) print(x)
これにより、リスト x のリストがシャッフルされます。シャッフルされたものを印刷しますresult.
注:
shuffle() は元のリストを変更するため、その後の操作やリストへのアクセスには shuffle によって加えられた変更が反映されることを覚えておくことが重要です。 ().
以上がPython の「random.shuffle()」が「None」を返すのはなぜですか?どうすれば正しく使用できますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

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

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

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

WebStorm Mac版
便利なJavaScript開発ツール
