logo

Python 結合リスト

このトピックでは、Python の異なる関数を使用して 2 つ以上のリストを結合する方法について説明します。概念を説明する前に、Python リストについて簡単に説明しましょう。あ Python リスト 同じ名前でグループ化された複数の項目のコレクションです。さまざまなデータ型 (整数、文字列、浮動小数点数など) の項目を (,) カンマで区切られた角括弧 [] 内に格納できます。

Python 結合リスト

Python リストを出力するプログラム

リスト.py

 # list of characters List1 = ['A', 'B', 'C', 'D', 'E'] # list of integers List2 = [1, 2, 3, 4, 5,] # mixed lists List3 = ['A', 1, 'C', 'E', 5, 8] print (' Display the List1 ', List1) print (' Display the List2 ', List2) print (' Display the List3 ', List3) 

出力

 Display the List1 ['A', 'B', 'C', 'D', 'E'] Display the List2 [1, 2, 3, 4, 5] Display the List3 ['A', 1, 'C', 'E', 5, 8] 

2 つ以上のリストを結合すると、 パイソン プログラムでは、結合されたリストが得られます。そして、このプロセスはリストの合成または結合と呼ばれます。

Python で 2 つ以上のリストを結合するさまざまな方法について説明します。

  • join() 関数と区切り文字を使用して Python でリストを結合する
  • 区切り文字を使用せずに join() 関数を使用して Python でリストを結合する
  • Pythonでmap()関数を使用して2つの整数リストを結合する
  • for ループと append() 関数を使用して Python で 2 つのリストを結合する
  • itertools.chain() メソッドを使用して Python で複数のリストを結合する
  • (+) プラス演算子を使用して Python で 2 つのリストを結合する
  • (*) 乗算またはアスタリスク演算子を使用して Python で 2 つのリストを結合します
  • Python で extend() 関数を使用して 2 つのリストを結合する

join() 関数を使用して Python でリストを結合する

参加する() 関数は、カンマ、記号、ハイフンなどの指定された区切り文字で区切られた、反復可能なリストを別のリストに結合するために使用されます。

構文

 str_name.join( iterable) 

文字列名: これは、反復可能なリストを区切る区切り文字の名前です。

反復可能: 一連の要素を含み、区切り文字で結合されたリストです。

戻り値: 指定された区切り文字で区切られた連結リストを返します。

注: 反復可能なリストに文字列以外の値または項目が含まれている場合は、TypeError 例外がスローされます。

join() 関数と区切り文字を使用して 2 つのリストを結合するプログラム

Join.py

 List1 = [ 'Apple', 'Orange', 'Banana', 'Mango', 'Grapes' ] Str2 = ', ' # It is the comma delimiter # use join() function to join List1 with the ' . ' delimiter Str2 = Str2.join( List1) # print the join list print (' Display the concatenated List1 using join() function and delimiter', Str2) List2 = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday' ] Str3 = ' - ' # It is the hyphen delimiter # use join() function to join List2 with the ' - ' delimiters Str3 = Str3.join( List2) # print the join list print (' Display the concatenated List2 using join() function and delimiter', Str3) 

出力

 Display the concatenated List1 using join() function and delimiter Apple, Orange, Banana, Mango, Grapes Display the concatenated List2 using join() function and delimiter Sunday - Monday - Tuesday - Wednesday - Thursday 

区切り文字を使わずにリストを結合するプログラム

プログパイ

 # declare a python list Lt1 = [ 'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't' ] print ( ' Display the elements of the List L1 ' , Lt1) L2 = ' ' # declare any empty string without defining any delimiter Ret = L2.join( Lt1) # use join method to join L1 list with L2 print ( ' Display the List without using delimiters', Ret) 

出力

 Display the elements of the List L1 ['j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't'] Display the List without using delimiters j a v a t p o i n t 

map() 関数を使用して 2 つの整数リストを結合する

整数リスト: これは、整数リストと呼ばれるリスト内のすべての整数を収集しますが、Python では join() 関数を使用して 2 つの整数リストを結合することはできません。したがって、私たちは 地図() 整数リストを文字列に変換する関数。その後、join() 関数を使用して、map() 関数の結果を適切な区切り文字で結合します。

構文:

 map(str, list_name) 

上記の構文では、map() 関数には list_name と str という 2 つのパラメーターがあります。 list_name は整数リストの名前で、str は文字列を表します。 map() 関数は list_name を文字列 (str) に変換します。

mysql 表示ユーザー

listでmap()関数とjoin()関数を使用するプログラム

map() 関数を使用して指定された整数リストを文字列に変換し、次に join() 関数を使用してリストを結合するプログラムを作成しましょう。

Convert.py

 lt = [1, 2, 3, 4, 5] # use map() function to convert integer list into string list_map = map(str, lt) lt2 = ', ' # use join() function to join lists and delimiter comma (,) res = lt2.join (list_map) print (' Display the concatenated integers list using map() and join() function ', res) 

出力

 Display the concatenated integers list using map() and join() function 1, 2, 3, 4, 5 

for ループと append() 関数を使用して Python で 2 つのリストを結合するプログラム

アン 追加する () 関数は、for ループを使用して、反復可能なリストの各要素を別のリストの最後に順番に追加または結合するために使用されます。 append() 関数を使用して、リストの要素を別のリストの末尾に追加する簡単なプログラムを作成してみましょう。

Append.py

 List1 = [1, 2, 3, 4, 5] # declare List1 List2 = [5, 6, 7, 8, 9, 10] # declare List2 print (' Given List1 ', List1) print (' Given List2 ', List2) # use for loop to iterate each element of Lt1 to l2 for i in List2: List1.append(i) # use append() function to insert each elements at the end of Lt1 print (' Display concatenation list using append() function ', List1) 

出力

 Given List1 [1, 2, 3, 4, 5] Given List2 [5, 6, 7, 8, 9, 10] Display concatenation list using append() function [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10] 

itertools.chain() メソッドを使用して複数のリストを結合するプログラム

Python で複数のリストを連結する簡単なプログラムを作成しましょう。 () メソッドをインポートして、 イターツール パッケージ。

新しい.py

 # use Python itertools.chain() method to join two list import itertools # declare different lists a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9, 10] c = [11, 12, 13, 14, 15] print (' Display the first list ', a) print (' Display the second list ', b) print (' Display the third list ', c) # use itertools.chain() method to join the list result = list (itertools.chain (a, b, c)) # pass the result variable in str() function to return the concatenated lists print (' Concatenated list in python using itertools.chain() method ', str (result)) 

出力

 Display the first list [1, 2, 3, 4, 5] Display the second list [6, 7, 8, 9, 10] Display the third list [11, 12, 13, 14, 15] Concatenated list in python using itertools.chain() method [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 

+ 演算子を使用して 2 つのリストを結合するプログラム

(+) プラス演算子を使用して Python で 2 つのリストを結合する例を考えてみましょう。

Pythonプログラミングの演算子

Mypro.py

 # Create a program to join two lists in Python using the '+' operator # declare two lists of characters list1 = [ 'A', 'B', 'C', 'D', 'E'] list2 = [ 'F', 'G', 'H', 'I', 'J'] # join two characters lists using '+' operator lt_sum1 = list1 + list2 # declares two lists of integers list3 = [ '1', '2', '3', '4', '5'] list4 = [ '6', '7', '8', '9', '10'] # join two integers lists using '+' operator lt_sum2 = list3 + list4 # display the concatenation list print (' Join two list of characters in Python using + operator: ', str(lt_sum1)) # display the concatenation list print (' Join two list of integers in Python using + operator: ', str(lt_sum2)) 

出力

 Join two list of characters in Python using + operator: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] Join two list of integers in Python using + operator: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] 

(*) 乗算演算子を使用して 2 つのリストを結合するプログラム

* 演算子を使用して Python で の 2 つのリストを結合する例を考えてみましょう。

Mypro2.py

 # declare two lists of characters List1 = [ 'A', 'B', 'C', 'D', 'E'] List2 = [ 'F', 'G', 'H', 'I', 'J'] print (' Display character List1 ', List1) print (' Display character List2 ', List2) # join two characters lists using '*' operator lt_sum1 = [*List1, *List2] # declares two lists of integers List3 = [ 1, 2, 3, 4, 5] List4 = [ 6, 7, 8, 9, 10] print (' Display integer List3 ', List3) print (' Display integer List4 ', List4) # join two integers lists using '*' operator lt_sum2 = [*List3, *List4] # display the concatenation list print (' Join two characters list in Python using * operator: '+ str(lt_sum1)) # display the concatenation list print (' Join two integers list in Python using * operator: '+ str(lt_sum2)) 

出力

 Display integer List3 [1, 2, 3, 4, 5] Display integer List4 [6, 7, 8, 9, 10] Join two characters list in Python using * operator: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] Join two integers list in Python using * operator: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

Python で extend() メソッドを使用して 2 つのリストを結合するプログラム

Python の extend() メソッドを使用して 2 つのリストを結合する簡単なプログラムを書いてみましょう。

プログパイ

 # takes two integers lists List1 = [5, 10, 5] List2 = [ 2, 4, 6, 8] print (' Display the List1 ', List1) print (' Display the List1 ', List2) # takes two string lists List3 = [ 'RED', 'BLUE', 'BLACK'] List4 = [ 'BROWN', 'PURPLE', 'GREY' ] print (' Display the List3 ', List3) print (' Display the List4 ', List4) # use extend() method to join two lists List1.extend(List2) List3.extend(List4) # print concatenation lists print( '
 Adding two lists of integers in Python using the extend() function: ', str(List1)) print( '
 Adding two lists of strings in Python using the extend() function: ', str(List3)) 

出力

 Display the List1 [5, 10, 5] Display the List1 [2, 4, 6, 8] Display the List3 ['RED', 'BLUE', 'BLACK'] Display the List4 ['BROWN', 'PURPLE', 'GREY'] Adding two lists of integers in Python using the extend() function: [5, 10, 5, 2, 4, 6, 8] Adding two lists of strings in Python using the extend() function: ['RED', 'BLUE', 'BLACK', 'BROWN', 'PURPLE', 'GREY']