logo

Pythonのzip()

Python zip() メソッド 反復可能なコンテナを受け取り、すべてのコンテナからマップされた値を含む単一の反復子オブジェクトを返します。

Python zip() 構文

慣れています 複数のコンテナの同様のインデックスをマップして、単一のエンティティを使用するだけでコンテナを使用できるようにします。



構文: zip(*イテレータ)

パラメーター : Python のイテラブルまたはコンテナ (リスト、文字列など)
戻り値: 単一のイテレータ オブジェクトを返します。

Python の zip() の例

Python zip() とリスト

パイソン 、zip() 関数は 2 つ以上を結合するために使用されます。 リスト (またはその他の反復可能)を単一の反復可能に変換し、対応する位置の要素をペアにします。結果として得られる反復可能な内容には、 タプル 、各リストの最初の要素がペアになり、各リストの 2 番目の要素がペアになる、というようになります。



Python3






name>=> [>'Manjeet'>,>'Nikhil'>,>'Shambhavi'>,>'Astha'> ]> roll_no>=> [>4>,>1>,>3>,>2> ]> # using zip() to map values> mapped>=> zip>(name, roll_no)> print>(>set>(mapped))>

>

>

出力

{('Nikhil', 1), ('Shambhavi', 3), ('Manjeet', 4), ('Astha', 2)}>

Python zip() と列挙

zip() と 列挙() は、複数のリストまたはタプルを並行して処理し、特定の目的でそれらのインデックスにアクセスする必要があるシナリオで役立ちます。

Python3


BFのための何か



names>=> [>'Mukesh'>,>'Roni'>,>'Chari'>]> ages>=> [>24>,>50>,>18>]> for> i, (name, age)>in> enumerate>(>zip>(names, ages)):> >print>(i, name, age)>

>

>

出力

0 Mukesh 24 1 Roni 50 2 Chari 18>

Python zip() と辞書

Python の zip() 関数は、2 つ以上の反復可能な関数を結合するために使用されます。 辞書 単一の反復可能オブジェクトに変換され、入力反復可能オブジェクトの対応する要素がタプルとしてペアにされます。辞書で zip() を使用すると、辞書内の位置に基づいて辞書のキーと値がペアになります。

Python3




stocks>=> [>'GEEKS'>,>'For'>,>'geeks'>]> prices>=> [>2175>,>1127>,>2750>]> new_dict>=> {stocks: prices>for> stocks,> >prices>in> zip>(stocks, prices)}> print>(new_dict)>

>

gimpで色を置き換える

>

出力

{'GEEKS': 2175, 'For': 1127, 'geeks': 2750}>

タプルを使用した Python zip()

タプルと一緒に使用すると、 ジップ() 位置に基づいてタプルの要素をペアにすることで機能します。結果の反復可能オブジェクトにはタプルが含まれており、i 番目のタプルには各入力タプルの i 番目の要素が含まれます。

Python3




tuple1>=> (>1>,>2>,>3>)> tuple2>=> (>'a'>,>'b'>,>'c'>)> zipped>=> zip>(tuple1, tuple2)> result>=> list>(zipped)> print>(result)>

>

>

出力:

[(1, 'a'), (2, 'b'), (3, 'c')]>

複数のイテラブルを含む Python zip()

Python の zip() 関数を使用して、3 つ以上の反復可能オブジェクトを結合することもできます。複数のイテラブルを入力として受け取り、タプルのイテラブルを返すことができます。各タプルには、入力イテラブルの対応する位置からの要素が含まれます。

Python3




list1>=> [>1>,>2>,>3>]> list2>=> [>'a'>,>'b'>,>'c'>]> list3>=> [>'x'>,>'y'>,>'z'>]> zipped>=> zip>(list1, list2, list3)> result>=> list>(zipped)> print>(result)>

>

>

出力

[(1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z')]>

サイズが等しくないリストを圧縮する

zip() 関数は、渡された最小のリストのみを反復処理します。異なる長さのリストが指定された場合、結果の組み合わせは、渡された最小のリストと同じ長さのみになります。次のコード例では:

C# 日時

Python3




# Define lists for 'persons', 'genders', and a tuple for 'ages'> persons>=> [>'Chandler'>,>'Monica'>,>'Ross'>,>'Rachel'>,>'Joey'>,>'Phoebe'>,>'Joanna'>]> genders>=> [>'Male'>,>'Female'>,>'Male'>,>'Female'>,>'Male'>,>'Female'>,>'Female'>]> ages>=> (>35>,>36>,>38>,>34>)> # Create a zipped object combining the 'persons' and 'genders'> #lists along with the 'ages' tuple> zipped_result>=> zip>(persons, genders, ages)> # Print the zipped object> print>(>'Zipped result as a list:'>)> for> i>in> list>(zipped_result):> >print>(i)>

>

>

出力

Zipped result as a list: ('Chandler', 'Male', 35) ('Monica', 'Female', 36) ('Ross', 'Male', 38) ('Rachel', 'Female', 34)>

zip()を使用した解凍

解凍とは、圧縮された値をそのままの状態で個別に変換することを意味します。これは次の助けを借りて行われます * オペレーター。

Python3




# initializing lists> name>=> [>'Manjeet'>,>'Nikhil'>,>'Shambhavi'>,>'Astha'>]> roll_no>=> [>4>,>1>,>3>,>2>]> marks>=> [>40>,>50>,>60>,>70>]> # using zip() to map values> mapped>=> zip>(name, roll_no, marks)> # converting values to print as list> mapped>=> list>(mapped)> # printing resultant values> print>(>'The zipped result is : '>, end>=>'')> print>(mapped)> print>(>' '>)> # unzipping values> namz, roll_noz, marksz>=> zip>(>*>mapped)> print>(>'The unzipped result: '>, end>=>'')> # printing initial lists> print>(>'The name list is : '>, end>=>'')> print>(namz)> print>(>'The roll_no list is : '>, end>=>'')> print>(roll_noz)> print>(>'The marks list is : '>, end>=>'')> print>(marksz)>

autocadストレッチコマンド

>

>

出力

The zipped result is : [('Manjeet', 4, 40), ('Nikhil', 1, 50), ('Shambhavi', 3, 60), ('Astha', 2, 70)] The unzipped result: The name list is : ('Manjeet', 'Nikhil', 'Shambhavi', 'Astha') The roll_no list is : (4, 1, 3, 2) The marks list is : (40, 50, 60, 70)>

Python ループで zip() を使用する

zip を使用して実行できるアプリケーションは数多くあります。 学生データベースまたはスコアカード またはグループのマッピングを必要とするその他のユーティリティ。スコアカードの小さな例を以下に示します。

Python3




# Python code to demonstrate the application of> # zip()> # initializing list of players.> players>=> [>'Sachin'>,>'Sehwag'>,>'Gambhir'>,>'Dravid'>,>'Raina'>]> # initializing their scores> scores>=> [>100>,>15>,>17>,>28>,>43>]> # printing players and scores.> for> pl, sc>in> zip>(players, scores):> >print>(>'Player : %s Score : %d'> %> (pl, sc))>

>

>

出力

Player : Sachin Score : 100 Player : Sehwag Score : 15 Player : Gambhir Score : 17 Player : Dravid Score : 28 Player : Raina Score : 43>