LoginSignup
2
0

Python学んでみた 第3回(リスト)

Last updated at Posted at 2024-03-03

はじめに

初めまして!
エンジニアになって数年、今まで本を読むだけでしたが、もっとプライベートで楽しみながら成長したい!自分が学んだ足跡を残していきたい!と思い記事を書きました!
最終的には自在に開発できるようになりたいと思っています。:triumph:
いろいろな記事を参考にさせてもらっています。:bow_tone2:
その中でもこれってどういう意味?とかつまづいたところを念入りに書いていこうかと思います。:fist:

今回の目的

前回の続きです。

使用したものや事前準備

・Macbook Pro
・Python
・Anaconda
・jupyter notebook

Python

リスト

Pythonの変数やリストの要素はオブジェクトID(オブジェクトへの参照)を保持している。(全部オブジェクトだから)
またリストのコピーはシャローコピー

names = ['taro','jiro',['saburo']]  # リストの作成
names.append('shirou')  # 末尾に追加
names  # ['taro', 'jiro', ['saburo'], 'shirou']
names2 = names  # 単純代入、オブジェクトを共有する(シャローコピーではない)
names3 = names.copy()  # 新たなリスト(別オブジェクト)を作成してシャローコピー(1階層のみコピー)
names is names2  # True(同じオブジェクトを共有している)
names is names3  # False(別オブジェクト)
names == names3  # True(内容は同じ)
names[1] = 'JIRO'  # 浅い層への代入、namesとnames2が大文字のJIROになる
names == names2  # True(namesとnames2は同じオブジェクトを参照しているので影響する)
names == names3  # False (namesとnames3は別オブジェクトなので影響しない)
names[2][0] = 'SABURO'  # 深い層への代入
names[2] == names3[2]  # True(深い層は共有しているので影響する)

基本的な操作

indexの取得

names = ['taro', 'jiro', 'saburo', 'shirou']
names.index('taro') # 0

値の変更

names[0] = 'Taro'
names # ['Taro', 'jiro', 'saburo', 'shirou']

リストの内に値があるか

'Taro' in names #true
'taro' in names #false

タプルの取得

names_tuple = list(enumerate(names))
names_tuple # [(0, 'Taro'), (1, 'jiro'), (2, 'saburo'), (3, 'shirou')]

負の位置のインデックスを持つタプルの取得

関数がありそうなので実際には使う機会少ないかも?

names_tuple_reverse = list((i - len(names), n)
    for i, n in enumerate(names))
names_tuple_reverse # [(-4, 'Taro'), (-3, 'jiro'), (-2, 'saburo'), (-1, 'shirou')]

スライス

スライスも半開区間(始まり含む)

names # ['Taro', 'jiro', 'saburo', 'shirou']
names[0:3] # ['Taro', 'jiro', 'saburo']
names[:3] # ['Taro', 'jiro', 'saburo']
names[3] # ['shirou']

ストライド

いくつか置きに値を取得。下の例は二つ置きに

names # ['Taro', 'jiro', 'saburo', 'shirou']
names[::2] # ['Taro', 'saburo']

逆順のストライド

names_tuple_reverse = list((i - len(names), n)
    for i, n in enumerate(names))
names_tuple_reverse # [(-4, 'Taro'), (-3, 'jiro'), (-2, 'saburo'), (-1, 'shirou')]
names_tuple_reverse[::-1] # [(-1, 'shirou'), (-2, 'saburo'), (-3, 'jiro'), (-4, 'Taro')]

リスト内包表記

通常の条件ループ

names = ['Taro', 'jiro', 'saburo', 'shirou']
names2 = []
for name in names:
    if len(name) == 4: # 4文字
        names2.append(name.title())
names2 # ['Taro', 'Jiro']

内包表記

リスト

names = ['Taro', 'jiro', 'saburo', 'shirou']
names2 = [name.title() for name in names if len(name) == 4]
names2 # ['Taro', 'Jiro']

辞書リスト

types = {'name':str,'age':int}
new_types = {t:t.title() for t in types}
new_types # {'name': 'Name', 'age': 'Age'}

辞書リスト(連想配列)

キーとバリューがあるリスト。ハッシュ値検索で行っている。そのため可変リストをキーにすることはできない。
3.7以降に順序保証されましたが、それ以前はされていないので注意

存在しないキーの対策

存在しないキーを入力するとエラーになる

types = {'name':str,'age':int}
types['food'] # エラー

下記で対策する

types = {'name':str,'age':int}
types.get('food','missing') # missingと表示されるのみ
'food' in types # False

続き

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0