python3

python3 でリストを結合する方法

投稿日:2020年12月5日 更新日:

2つのリストを結合して、「リストのリストを作りたい場合」と、「1つの長いリストを作りたい」場合がある。


それぞれ、次のようにする。

# リスト2つを用意
l_1 = [1,2,3]
l_2 = [4,5,6]

l = [] # リストのリストを作る
l.append(l_1)
l.append(l_2)
print(str(l))

l = l_1 + l_2 # 結合リストを作る
print(str(l))

結果

[[1, 2, 3], [4, 5, 6]]
[1, 2, 3, 4, 5, 6]

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で、複数のグラフを簡単にプロットする方法

変数を並べて書くと、次のように自動的に順番を理解してグラフを表示してくれる。 例 import numpy as np from matplotlib import pyplot as plt fro …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

next() を使う。 例 my_arr = [‘春’,’夏’,’秋’,’冬’] my_iter = iter(my_arr) print(next(my_iter,’なし’)) print(next …

no image

python3 で文字列を入力させて受け取る方法

C言語で scanf のようなものを作るには、python では input() で実現できる。 例 print(‘xの値を入力してください’) x = input() print(‘xの値は ‘ + …

no image

matplotlib で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …

no image

matplotlib の ax でx軸、y軸の端の値を設定する

axes には、set_xlim として定義する。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace( …