リスト内包表記
リスト内包表記は、既存のリストの値に基づいて新しいリストを作成する場合に、より短い構文を提供します。 (参照-https://www.w3schools.com/python/python_lists_comprehension.asp)
例:1
方法:1
fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [] for x in fruits: if "a" in x: newlist.append(x) print(newlist)
方法:2(総合)
fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [x for x in fruits if "a" in x] print(newlist)
出力:
['apple', 'banana', 'mango']
例:2
l = [10,20,30,40] newlist = [] #using normal loop for num in l: newlist.append(num**2) print(newlist) #using loop in comprehensive way newlist = [num**2 for num in l] print(newlist)
出力:
[100, 400, 900, 1600] [100, 400, 900, 1600]
エクササイズ:
1. 2 つのリストから類似した番号と、同じ 2 つのリストから異なる番号を見つけます。
l1 = [10,20,30,40]
l2 = [30,40,50,60]
次の出力を取得します:
a) 30,40
#30,40 l1 = [10,20,30,40] l2 = [30,40,50,60] #normal method for num in l1: for no in l2: if num== no: print(num,end=' ') #comprehensive print([num for num in l1 for no in l2 if num==no])
出力:
[30, 40]
b) 10、20、50、60
l1 = [10,20,30,40] l2 = [30,40,50,60] #comprehensive output = [num for num in l1 if num not in l2] output = output + [num for num in l2 if num not in l1] print(output) #normal method for num in l1: if num not in l2: print(num,end=' ') for num in l2: if num not in l1: print(num,end=' ')
出力:
[10, 20, 50, 60] 10 20 50 60
2.指定された出力に対するプログラムを包括的なアプローチで検索します
l1 = [1,2,3]
l2 = [5,6,7]
出力:[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6) 、(3、7)]
l1 = [1,2,3] l2 = [5,6,7] l = [(i,j) for i in l1 for j in l2 if i!=j] print(l)
出力:
[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)]
3.指定された出力のプログラムを検索します:
s = "a1b2c3"
出力: abc123
方法:1
s = "a1b2c3" alpha_list = [] num_list = [] for letter in s: if letter.isalpha(): alpha_list.append(letter) else: num_list.append(letter) print("".join(alpha_list+num_list))
方法:2
s = "a1b2c3" letter=''.join([i for i in s if i.isalpha()]) no=''.join([i for i in s if i.isdigit()]) print(letter+no)
出力:
abc123
4.指定された出力のプログラムを検索します:
s = "a4k3b2"
出力: aeknbd
s = "a4k3b2" i = 0 while i<len first="s[i]" second="int(s[i+1])" print chr i> <p>出力:</p> <p>aeknbd</p> <p><strong>説明:</strong></p> <p>-->first の ASCII 値は ord(first) を使用して取得され、それに Second が追加されて新しい文字が見つかります。<br> -->ord() は ASCII 値を検索するために使用されます。<br> -->chr() は ASCII 値を変換します-->文字。</p> </len>
以上がPython Day-List の理解-演習の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

inpython、youappendelementStoalistusingtheappend()method.1)useappend()forsingleelements:my_list.append(4).2)useextend()or = formultipleElements:my_list.extend(another_list)ormy_list = [4,5,6] .3)forspecificpositions:my_list.insert(1,5).beaware

シェバンの問題をデバッグする方法には次のものがあります。1。シバン行をチェックして、それがスクリプトの最初の行であり、接頭辞スペースがないことを確認します。 2.通訳パスが正しいかどうかを確認します。 3.通訳を直接呼び出してスクリプトを実行して、シェバンの問題を分離します。 4. StraceまたはTrustsを使用して、システムコールを追跡します。 5.シバンに対する環境変数の影響を確認してください。

pythonlistscanbemanipulatedsingseveralmethodstoremoveElements:1)theremove()methodremovesthefirstoccurrenceofaspecifiedValue.2)thepop()methop()methodremovessanelementatagivenindex.3)thedelstatementementementementementementementementementemoritemoricedex.4)

Integers、strings、floats、booleans、otherlists、anddictionaryを含むpythonlistscanstoreanydatype

PythonListsSupportNumersoperations:1)AddingElementSwithAppend()、Extend()、Andinert()

Numpyを使用して多次元配列を作成すると、次の手順を通じて実現できます。1)numpy.array()関数を使用して、np.array([[1,2,3]、[4,5,6]])などの配列を作成して2D配列を作成します。 2)np.zeros()、np.ones()、np.random.random()およびその他の関数を使用して、特定の値で満たされた配列を作成します。 3)アレイの形状とサイズの特性を理解して、サブアレイの長さが一貫していることを確認し、エラーを回避します。 4)np.reshape()関数を使用して、配列の形状を変更します。 5)コードが明確で効率的であることを確認するために、メモリの使用に注意してください。

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

Forpythondatastorage、chooseLists forfficability withmixeddatypes、array.arrayformemory-efficienthogeneousnumericaldata、およびnumpyArrays foradvancednumericalcomputing.listSareversatilebuteficient efficient forlargeNumericaldatates;


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

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

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

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

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

ホットトピック









