配列・リスト

リストのスライス

l = [
    {'reviewAverage': 3.6, 'reviewCount': 352},
    {'reviewAverage': 2.4, 'reviewCount': 164},
    {'reviewAverage': 1.9, 'reviewCount': 164},
    {'reviewAverage': 4.2, 'reviewCount': 1257},
    {'reviewAverage': 5.0, 'reviewCount': 29},
    {'reviewAverage': 3.8, 'reviewCount': 874},
]

l = l[:3]
pprint.pprint(l)
# [{'reviewAverage': 3.6, 'reviewCount': 352},
#  {'reviewAverage': 2.4, 'reviewCount': 164},
#  {'reviewAverage': 1.9, 'reviewCount': 164}]

リストに要素を追加

l = ['A', 'B']
l.append('add')

print(l)
# ['A', 'B', 'add']
add = ['add']
l = ['A', 'B'] + add

print(l)
# ['A', 'B', 'add']

リスト内包表記

l = [1,2,3,4,5]
l_1 = []

for i in l:
    l_1.append(str(i))

print(l_1)
# ['1', '2', '3', '4', '5']

# リスト内包表記
l_2 = [str(i) for i in l]

print(l_2)
# ['1', '2', '3', '4', '5']

配列にネストした配列を複数追加

  • 以下のように書くのは冗長
parent = []

child_1 = [1, 2, 3]
child_2 = [4, 5, 6]
child_3 = [7, 8, 9]

parent.append(child_1)
parent.append(child_2)
parent.append(child_3)

print(parent)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
parent = []

child_1 = [1, 2, 3]
child_2 = [4, 5, 6]
child_3 = [7, 8, 9]

parent += [child_1, child_2, child_3]

print(parent)
  • 新たに空の配列を作るなら
child_1 = [1, 2, 3]
child_2 = [4, 5, 6]
child_3 = [7, 8, 9]

parent = [child_1, child_2, child_3]

print(parent)

リストの先頭に要素を追加

val = [10, 'a']
add = 'add'
val.insert(0, add)
print(val)
# ['add', 10, 'a']

リストに空の要素を追加

l = ['S', 860, 'M', 950]
l += [''] * 6

print(l)
# ['S', 860, 'M', 950, '', '', '', '', '', '']

リスト同士の結合

list_merge = []
list1 = [1, 2, 3]
list2 = [4, 5, 6]

list_merge += list1
list_merge += list2

print(list_merge)
# [1, 2, 3, 4, 5, 6]
list_merge = [[1, 2, 3], [4, 5, 6]]
list_merge2 = []

for lst in list_merge:
    for l in lst:
        list_merge2.append(l)

print(list_merge2)

リストの中身の連結

文字列のリストを連結

l = ['aaa', 'bbb', 'ccc']

s = ''.join(l)
print(s)
# aaabbbccc

s = ','.join(l)
print(s)
# aaa,bbb,ccc

s = '-'.join(l)
print(s)
# aaa-bbb-ccc

s = '\n'.join(l)
print(s)
# aaa
# bbb
# ccc

数値のリストを文字列として連結

  • join()の引数に文字列以外を要素とするリストを指定するとエラーとなる。
l = [0, 1, 2]

s = '-'.join([str(n) for n in l])
print(s)
# 0-1-2

リストから重複した要素を削除

l = [3, 3, 2, 1, 5, 1, 4, 2, 3]

print(list(dict.fromkeys(l)))
# [3, 2, 1, 5, 4]

リストをソート

l = ['header.php', 'sidebar.php', 'content.php', 'modal.php', 'code.php']
l.sort()
print(l)
# ['code.php', 'content.php', 'header.php', 'modal.php', 'sidebar.php']

リストの中身に連番をふる

l_content = ['a', 'b', 'c', 'd', 'e']

countNum = 1
for l in l_content:
    print(l + '-' + str(countNum))
    countNum += 1
# a-1
# b-2
# c-3
# d-4
# e-5
l_content = ['a', 'b', 'c', 'd', 'e']
l_len = len(l_content)
print(l_len)
# 5

for lc, ll in zip(l_content, range(1, l_len + 1)):
    print(lc + '-' + str(ll))
# a-1
# b-2
# c-3
# d-4
# e-5

リストから数値のみ取り出す

l = [1000, 'S', 860, 'M', 950]
s = []

for i in l:
    if type(i) is int:
        s.append(i)

print(s)
# [1000, 860, 950]

リストから最小値・最大値を取り出す

s = ['Z', 'A', 'B', 'C']
i = [1000, 860, 950]

min_s = min(s)
print(min_s)
# A

min_i = min(i)
print(min_i)
# 860
  • 最大値は max() で取得

リスト内の要素の存在を判定 in / not in

l = [0, 1, 2]
i = 0

if i in l:
    print('true')
l = [1,2,3,4,5,6,7,8,9]
a = [1,2,3]
b = [4, 5]

ab = a + b
print(ab)
# [1, 2, 3, 4, 5]

l2 = []

for n in l:
    if n not in ab:
        l2.append(n)

print(l2)
# [6, 7, 8, 9]

特定の文字列を含む配列の要素を抽出

l = ['aaa', 'bbbX', 'ccc', 'dddX']

elm_in = [s for s in l if 'X' in s]
print(elm_in)
# ['bbbX', 'dddX']

elm_not_in = [s for s in l if 'X' not in s]
print(elm_not_in)
# ['aaa', 'ccc']

リストの中身をランダムに取り出す

import random

l = ['a', 'b', 'c', 'd', 'e']

r = l[random.randrange(0, len(l), 1)]

print(r)

split() 文字列を分割してリスト化

s = 'one two three'
l = s.split()
print(l)
# ['one', 'two', 'three']

s = 'one two        three'
l = s.split()
print(l)
# ['one', 'two', 'three']

s = 'one\ttwo\tthree'
l = s.split()
print(l)
# ['one', 'two', 'three']

リストを辞書に変換

l = ['JS', 'JavaScript']
d = {l[0]: l[1]}
print(d)
# {'JS': 'JavaScript'}

2つのリストを辞書に変換

l1 = ['a', 'b', 'c']
l2 = [1, 2, 3]

d = dict(zip(l1, l2))
print(d)
# {'a': 1, 'b': 2, 'c': 3}

リスト内の空の要素を除外する

l = [1, '', 3, '', 5]
l = [i for i in l if i != '']

print(l)
# [1, 3, 5]

多重配列の中身を順番に取り出す

  • オブジェクトの型で判定する場合、is 型 で判定できる
    • is str
    • is int
    • is list

Pythonで型を取得・判定するtype関数, isinstance関数 | note.nkmk.me

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

for i in l:
    if type(i) is list:
        for e in i:
            print(e)
    else:
        print(i)