Python では、特定の値に基づいてリストをサブリストに分割します。このアイデアは、指定された値が見つかるたびに元のリストを反復処理し、要素をサブリストにグループ化することです。特に大量のデータを扱う場合には、リストの操作と処理が必要になることがよくあります。よく行われる操作の 1 つは、特定の値に従ってリストを複数のサブリストに分割することです。コンポーネントを結合したり、データのさまざまなサブセットを分析したりする場合、この手順が役立つことがあります。
mysqlと等しくない
条件に基づいてリストをリストに分割する方法
ここでは、指定された条件に基づいてリストをリストに分割するために使用できるさまざまな方法を示します。
- 単純な反復の使用
- リスト内包表記の使用
- for ループの使用
- 再帰の使用
- Itertoolsの使用
- NumPy の使用
Python で反復を使用してリストを分割する
で パイソン を使用して、リストを特定の値ごとにリストに分割します。 単純な反復 。このコードはリストと特定の値を初期化します。次に、リストの各要素を反復処理することにより、特定の値に基づいてリストをサブリストに分割します。
Python3
test_list>=> [>1>,>4>,>5>,>6>,>4>,>5>,>6>,>5>,>4>]> print>(>'The original list : '> +> str>(test_list))> particular_value>=> 5> result>=> []> temp_list>=> []> for> i>in> test_list:> >if> i>=>=> particular_value:> >temp_list.append(i)> >result.append(temp_list)> >temp_list>=> []> >else>:> >temp_list.append(i)> result.append(temp_list)> print>(>'The list after splitting by a value : '> +> str>(result))> |
>
>
出力
The original list : [1, 4, 5, 6, 4, 5, 6, 5, 4] The list after splitting by a value : [[1, 4, 5], [6, 4, 5], [6, 5], [4]]>
リスト内包表記を使用して Python リストをチャンクに分割する
Python では、次を使用してリストを特定の値ごとにリストに分割します。 リストの内包表記 。この問題は 2 つの部分で解決できます。最初の部分では、次を使用して分割を実行する必要があるインデックス リストを取得します。 列挙する 関数。次に、次を使用してインデックスに従って値を結合できます。 ジップ そしてリストのスライス。
Python3
test_list>=> [>1>,>4>,>5>,>6>,>4>,>5>,>6>,>5>,>4>]> print>(>'The original list : '> +> str>(test_list))> # using list comprehension Split list into lists by particular value> size>=> len>(test_list)> idx_list>=> [idx>+> 1> for> idx, val>in> >enumerate>(test_list)>if> val>=>=> 5>]> res>=> [test_list[i: j]>for> i, j>in> >zip>([>0>]>+> idx_list, idx_list>+> >([size]>if> idx_list[>->1>] !>=> size>else> []))]> print>(>'The list after splitting by a value : '> +> str>(res))> |
>
>
出力
The original list : [1, 4, 5, 6, 4, 5, 6, 5, 4] The list after splitting by a value : [[1, 4, 5], [6, 4, 5], [6, 5], [4]]>
For ループを使用して Python でリストを分割する
Python では、次を使用してリストを特定の値ごとにリストに分割します。 for ループ s.このコードは、元のリストを文字列表現に変換し、特定の価格を区切り文字 (*) に置き換えます。次に、変更されたものを分割します 弦 区切り文字で部分文字列を取得します。
Python3
test_list>=> [>1>,>4>,>5>,>6>,>4>,>5>,>6>,>5>,>4>]> print>(>'The original list : '> +> str>(test_list))> x>=> list>(>map>(>str>, test_list))> x>=> ' '>.join(x)> x>=> x.replace(>'5'>,>'5*'>)> y>=> x.split(>'*'>)> res>=> []> for> i>in> y:> >i>=> i.strip()> >i>=> i.split(>' '>)> >b>=> []> >for> j>in> i:> >b.append(>int>(j))> >res.append(b)> print>(>'The list after splitting by a value : '> +> str>(res))> |
>
>
出力
The original list : [1, 4, 5, 6, 4, 5, 6, 5, 4] The list after splitting by a value : [[1, 4, 5], [6, 4, 5], [6, 5], [4]]>
再帰を使用してリストを複数のリストに分割する
Python では、次を使用してリストを特定の値ごとにリストに分割します。 再帰 。コードは再帰的なものを定義しています 関数 これは、split_list_recursive と呼ばれ、特定の値が発生するたびに指定されたリストをサブリストに分割します。再帰と 2 つの一時リスト (result と temp_list) を使用して、結果のサブリストを保存します。
Python3
def> split_list_recursive(test_list, result, temp_list, particular_value):> >if> not> test_list:> >result.append(temp_list)> >return> >if> test_list[>0>]>=>=> particular_value:> >result.append(temp_list>+> [particular_value])> >split_list_recursive(test_list[>1>:], result, [], particular_value)> >else>:> >split_list_recursive(test_list[>1>:],> >result,> >temp_list>+> [test_list[>0>]],> >particular_value)> test_list>=> [>1>,>4>,>5>,>6>,>4>,>5>,>6>,>5>,>4>]> particular_value>=> 5> result>=> []> print>(>'The original list:'>, test_list)> split_list_recursive(test_list, result, [], particular_value)> print>(>'The list after splitting by value:'>, result)> |
>
>
出力
The original list: [1, 4, 5, 6, 4, 5, 6, 5, 4] The list after splitting by value: [[1, 4, 5], [6, 4, 5], [6, 5], [4]]>
Itertoolsを使用してPythonでリストを分割する
Python では、次を使用してリストを特定の値ごとにリストに分割します。 イターツール 。このコードは、リストと値を入力として受け取る、split_list という関数を定義します。それは、 itertools.groupby() 連続する要素をグループ化する関数 リスト それらが指定された値と等しいかどうかに基づきます。
Python3
import> itertools> def> split_list(lst, val):> >return> [>list>(group)>for> k,> >group>in> >itertools.groupby(lst,>lambda> x: x>=>=>val)>if> not> k]> original_lst>=> [>1>,>4>,>5>,>6>,>4>,>5>,>6>,>5>,>4>]> split_lst>=> split_list(original_lst,>6>)> print>(>'The original list:'>, original_lst)> print>(>'The list after splitting by a value:'>, split_lst)> |
>
>
出力
The original list: [1, 4, 5, 6, 4, 5, 6, 5, 4] The list after splitting by a value: [[1, 4, 5], [4, 5], [5, 4]]>
NumPyを使用してPythonでリストを分割する
Python では、リストを次のように分割します。 リスト Numpyを使用して特定の値によって。このコードは、リストと特定の値を入力として受け取ります。次に、リストを ナムピー 配列を検索し、特定の値が出現するインデックスを見つけます。
Python3
import> numpy as np> test_list>=> [>1>,>4>,>5>,>6>,>4>,>5>,>6>,>5>,>4>]> particular_value>=> 5> arr>=> np.array(test_list)> idx>=> np.where(arr>=>=> particular_value)[>0>]> subarrays>=> np.split(arr, idx>+>1>)> result>=> [subarray.tolist()>for> subarray>in> subarrays>if> len>(subarray)>>>0>]> print>(>'The original list:'>, test_list)> print>(>'The list after splitting by a value:'>, result)> |
>
>
出力
The original list: [1, 4, 5, 6, 4, 5, 6, 5, 4] The list after splitting by a value: [[1, 4, 5], [6, 4, 5], [6, 5], [4]>