719599 ランダム
 ホーム | 日記 | プロフィール 【フォローする】 【ログイン】

mlupinのブログ

mlupinのブログ

【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! --/--
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

PR

キーワードサーチ

▼キーワード検索

カレンダー

コメント新着

aki@ Re:今朝の朝食は、じゃがいものポタージュスープ(01/20) この様な書込大変失礼致します。日本も当…
aki@ Re:今朝の朝食は、じゃがいものポタージュスープ(01/20) この様な書込大変失礼致します。日本も当…
mlupin@ Re:いちごの苗2023 その後 花が?(12/21) かずパパさん、コメントありがとうござい…
かずパパ@ Re:いちごの苗2023 その後 花が?(12/21) こんにちは、藻緯羅さんのブログから来ま…
mlupin@ Re[1]:今朝の朝食は、食事系のパンケーキ(11/26) 藻緯羅さんへ いつもコメントありがとうご…

カテゴリ

バックナンバー

2022.05.13
XML
カテゴリ:Python
# 「Pythonことはじめ」 その6

# 特集4 Pythonことはじめ
#
#『日経ソフトウエア』2018年07月号(p.76~p.86)
#
# 目次
#
# リスト、タプル、辞書、集合:データをまとめて扱う仕組み(p.84)
# 辞書をループで一括処理するプログラム(p.82)
#
# 〇リストの例
# リストは、数列を「[]」(角かっこ)で囲む。
ints = [29, 6, 45, 131, 10, 254]
print('ints = ', ints)
# 最初の数「29」を出力
print('ints[0] =', ints[0])
#
# 〇タプルの例
print('〇タプルの例')
# タプルは、数列を「()」(丸かっこ)で囲む。
tints = (29, 6, 45, 131, 10, 254)
print('tints = ', tints)
# 最初の数「29」を出力
print('tints[0] =', tints[0])
#
#〇リストとタプルの違い
# リストは、データの変更や追加、削除ができるが、タプルはできない。
print('①リストは、データの変更ができる。')
print(' 4番目の要素を「0」に変更する。')
ints[3] = 0
print('ints = ', ints)
#
print('②リストは、「append関数」でデータを末尾に追加することができる。')
print('ints.append(77)')
ints.append(77)
print('ints = ', ints)
print('③リストは、「del関数」でデータを削除することができる。')
print(' 3番目の要素「45」を削除する。')
print('del(ints[2])')
del(ints[2])
print('ints = ', ints)
#
print('タプルで要素を変更しようとするとエラーになる。')
tints[3] = 0
# 〇辞書の例
print('〇辞書の例')
# 「辞書」は、「キー」と「データ」を「:」でペアにした文字列を「{}」(波かっこ)で囲む。
# 各ペアは、「,(カンマ)」で区切って並べる。
colors = {'red':'apple', 'green':'melon', 'blue':'blueberry'}
print('colors = ', colors)
# 辞書から特定のデータを取り出す。
print('辞書から特定のデータを取り出す。')
# 「melon」を出力
print("colors['green'] =", colors['green']
# 辞書のデータを変更。
print('辞書のデータを変更。')
# colors['red']を「strawberry」に変更。
colors['red']  = 'strawberry'
print("colors['red'] =", colors['red']
# 辞書にデータのペアを追加。
print('辞書にデータのペアを追加。')
# 'yellow'と’banana'のペアを追加。
colors['yellow']  = 'banana'
print("colors['yellow' =", colors['yellow']
# 辞書からデータを削除。
print('辞書からデータを削除。')
print("'blue'と’blueberry'のペアを削除。")
del(colors['blue'] )
print("colors =", colors
# 「負のインデックス」の例
print('「負のインデックス」の例)
print("'blue'と’blueberry'のペアを削除。")
del(colors['blue'] )
print("colors =", colors)
# 〇「スライシング」機能
print('〇「スライシング」機能の例')
print(' 2番目から4番目までのデータを切り取りたい場合')
ints = [29, 6, 45, 131, 10, 254]
print('ints = ', ints)
print('print(ints[1:4]) -> ', ints[1:4])
# リストの一部から新しいリストを作成する。 
print(' リストの一部から新しいリストを作成する。')
ints2 = ints[1:4]
print('ints2 = ints[1:4]')
print('print(ints2) -> ', ints2)
# 〇「負のインデックス」と「スライシング」を組み合わせる。
# 最後のデータを取り除いた新しいリストを作成する。 
print('〇「負のインデックス」と「スライシング」を組み合わせる。')
print(' 最後のデータを取り除いた新しいリストを作成する。')
print('ints = ', ints)
rem_e = ints[:-1]
print('rem_e = ints[:-1]')
print(' [:]の前には何もないが、何もない場合は「0」を表す。「0」の場合は省略可能')
print('print("rem_e") -> ', rem_e)
# 最後のデータを取り除いた新しいリストを作成する。 
print(' 最後のデータを取り除いた新しいリストを作成する。')
print('ints = ', ints)
rem_e = ints[:-1]
print('rem_e = ints[:-1]')
print('print(rem_e =) -> ', rem_e)
# データを1個とびに取り出して、かつ最後のデータを除外した新しいリストを作成する。 
print(' データを1個とびに取り出して、かつ最後のデータを除外した新しいリストを作成する。')
print('ints = ', ints)
rem_e2 = ints[:-1:2]
print('rem_e2 = ints[:-1:2]')
print('print(rem_e2) -> ', rem_e2)
print(' ints[:-1:2]の最後の2は「1個飛び」を意味する。')
print(' 2個飛びにしたいのであれば、ints[:-1:3]にすれば良い。')

●上記の実行結果
〇リストの例
リストは、数列を「[]」(角かっこ)で囲む。
ints =  [29, 6, 45, 131, 10, 254]
最初の数「29」を出力
ints[0] = 29
〇タプルの例
タプルは、数列を「()」(丸かっこ)で囲む。
tints =  (29, 6, 45, 131, 10, 254)
最初の数「29」を出力
tints[0] = 29
リストは、データの変更や追加、削除ができるが、タプルはできない。
①リストは、データの変更ができる。
 4番目の要素「131」を「0」に変更する。
ints[3] = 0
ints =  [29, 6, 45, 0, 10, 254]
②リストは、「append関数」でデータを末尾に追加することができる。
ints.append(77)
ints =  [29, 6, 45, 0, 10, 254, 77]
③リストは、「del関数」でデータを削除することができる。
 3番目の要素「45」を削除する。
del(ints[2])
ints =  [29, 6, 0, 10, 254, 77]
 タプルで要素を変更しようとするとエラーになる。
 tints[3] = 0 を実行しようとするとエラーになる。
〇辞書の例
 「辞書」は、「キー」と「データ」を「:」でペアにした文字列を「{}」(波かっこ)で囲む。
 各ペアは、「,(カンマ)」で区切って並べる。
colors =  {'red': 'apple', 'green': 'melon', 'blue': 'blueberry'}
 辞書から特定のデータを取り出す。
 辞書から特定のデータを取り出す。
 「melon」を出力
colors['green'] = melon
 辞書のデータを変更。
colors['red'] = strawberry
colors =  {'red': 'strawberry', 'green': 'melon', 'blue': 'blueberry'}
 辞書にデータのペアを追加。
colors['yellow'] = banana
colors =  {'red': 'strawberry', 'green': 'melon', 'blue': 'blueberry', 'yellow': 'banana'}
 辞書からデータを削除。
 'blue'と’blueberry'のペアを削除。
del(colors['blue'] )
colors = {'red': 'strawberry', 'green': 'melon', 'yellow': 'banana'}
〇「負のインデックス」の例
ints =  [29, 6, 45, 131, 10, 254]
ints[-2] =  10
〇「スライシング」機能の例
 2番目から4番目までのデータを切り取りたい場合
ints =  [29, 6, 45, 131, 10, 254]
print(ints[1:4]) ->  [6, 45, 131]
 リストの一部から新しいリストを作成する。
ints2 = ints[1:4]
print(ints2) ->  [6, 45, 131]
〇「負のインデックス」と「スライシング」を組み合わせる。
 最後のデータを取り除いた新しいリストを作成する。
ints =  [29, 6, 45, 131, 10, 254]
rem_e = ints[:-1]
 [:]の前には何もないが、何もない場合は「0」を表す。「0」の場合は省略可能
print("rem_e") ->  [29, 6, 45, 131, 10]
 最後のデータを取り除いた新しいリストを作成する。
ints =  [29, 6, 45, 131, 10, 254]
rem_e = ints[:-1]
print(rem_e =) ->  [29, 6, 45, 131, 10]
 データを1個とびに取り出して、かつ最後のデータを除外した新しいリストを作成する。
ints =  [29, 6, 45, 131, 10, 254]
rem_e2 = ints[:-1:2]
print(rem_e2) ->  [29, 45, 10]
 ints[:-1:2]の最後の2は「1個飛び」を意味する。
 2個飛びにしたいのであれば、ints[:-1:3]にすれば良い。





お気に入りの記事を「いいね!」で応援しよう

最終更新日  2022.05.13 15:41:34
コメント(0) | コメントを書く



© Rakuten Group, Inc.