python3

python3 で、リストを pop したものの返り値

投稿日:

リストを pop すると、 pop された値が返される。

plant_1 = ['pumpkin', 'ginger', 'potato']
plant_2 = []

print('plant_1 is ' + str(plant_1))
print('plant_2 is ' + str(plant_2))
plant_2.append(plant_1.pop())

print('============')

print('plant_1 is ' + str(plant_1))
print('plant_2 is ' + str(plant_2))

結果

plant_1 is ['pumpkin', 'ginger', 'potato']
plant_2 is []
============
plant_1 is ['pumpkin', 'ginger']
plant_2 is ['potato']

-python3
-

執筆者:


comment

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

関連記事

no image

python3 で、csv ファイルの日付・数字を読み取る方法

csv をインポートすると、row in reader はリストになっている。 次のようにして、各行の要素を取り出して表示することができる。 例 import csv with open(‘data. …

no image

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

2つのリストを結合して、「リストのリストを作りたい場合」と、「1つの長いリストを作りたい」場合がある。 それぞれ、次のようにする。 例 # リスト2つを用意 l_1 = [1,2,3] l_2 …

no image

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

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

no image

matplotlib で目盛りに文字を使用する方法

文字に tex の記法を使って数式を入力することもできる。 例 import numpy as np from matplotlib import pyplot as plt x = np.linsp …

no image

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

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