複数の or 演算子が予期しない結果を生成する理由
Python では、複数の or 演算子を含む式を評価すると誤解を招く可能性があります。たとえば、次のコード行を考えてみましょう。
if name == "Kevin" or "Jon" or "Inbar":
この行は、権限のないユーザーにアクセスを許可することを意図していますが、実際にはすべてのユーザーにアクセスを許可します。その理由を理解するには、演算子の動作を調べる必要があります。
複数の Or の問題
英語では、「or」は複数の節を接続し、少なくとも 1 つの節を意味します。それは真実でなければなりません。ただし、Python では、「A or B or C」のような式は「(A or B) or C」として解析されます。これは、最初の式のみが評価されることを意味します。 true の場合、式全体が true です。
解決策 1: 複数の等価演算子
「名前」を各許可ユーザーと正しく比較するには、複数の等価演算子を使用します。 :
if name == "Kevin" or name == "Jon" or name == "Inbar":
解決策 2: 設定するメンバーシップ
別の解決策は、承認された名前のセットを使用してメンバーシップを確認することです:
authorized = {"Kevin", "Jon", "Inbar"} if name in authorized:
解決策 3: 任意のオペレータ
any() 関数は、承認された名前を反復処理し、それらのいずれかが一致する場合に True を返すために使用することもできます。 "name":
authorized = ["Kevin", "Jon", "Inbar"] if any(name == auth for auth in authorized):
パフォーマンス
3 つのソリューションのうち、セット メンバーシップが最高のパフォーマンスを提供し、次に複数の等価演算子が続きます。 any() を使用するのが最も効率的ではありません。
以上がPython で複数の「or」演算子が予期しないブール結果を生成するのはなぜですか?の詳細内容です。詳細については、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 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

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

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









