[Python] 配列の使い方

Pythonでの配列の使い方メモ。

配列の宣言・要素の追加

配列を宣言

colors = ['red', 'green']

要素を追加

appendメソッドを使って要素を追加します。

colors = []

colors.append('red')
print colors
# ['red'] 

colors.append('green')
print colors
# ['red', 'green']

要素の取得

index値を指定して取得

colors = ['red', 'green', 'blue', 'yellow']

print colors[0]
# red

print colors[2]
# blue

最後の要素を取得

index値として-1を指定すると最後の要素を、更にマイナス値を増やすと逆順に要素を取得します。

colors = ['red', 'green', 'blue', 'yellow']

print colors[-1]
# yellow

print colors[-2]
# blue

配列の結合

結合結果を上書き

extendメソッドを使って2つの配列を結合します。

colors1 = ['red', 'green']
colors2 = ['blue', 'yellow']

# 配列1に配列2を結合
colors1.extend(colors2)

print colors1
# ['red', 'green', 'blue', 'yellow']

新規配列変数に結合する

元の配列値は残したい場合。配列を加算して2つの配列を結合します。

colors1 = ['red', 'green']
colors2 = ['blue', 'yellow']

# 配列1と配列2を結合した配列3を生成
colors_new = colors1 + colors2

print colors_new
# ['red', 'green', 'blue', 'yellow']

配列要素の引き算

old_ids = [1, 2, 4, 5]
new_ids = [1, 3, 4]

print list(set(old_ids) - set(new_ids))
# [2, 5]

print list(set(new_ids) - set(old_ids))
# [3]

配列要素分ループさせる

要素のみ取得するループ

colors = ['red', 'green', 'blue', 'yellow']

for color in colors:
    print color

# red
# green
# blue
# yellow

要素とインデックス値を取得するループ

colors = ['red', 'green', 'blue', 'yellow']

for i, color in enumerate(colors):
    print i, '-->', color

# 0 --> red
# 1 --> green
# 2 --> blue
# 3 --> yellow

逆順ループ

colors = ['red', 'green', 'blue', 'yellow']

for color in reversed(colors):
    print color

# yellow
# blue
# green
# red

配列要素が条件を満たしているか判定

1要素でもTrueが存在しているかの判定はany関数、全ての要素がTrueかどうかの判定はall関数で行う。

test = [True, False, True]

any(test)
# True

all(test)
# False

実際のコード上だと、もう少し複雑な元データになると思うので、まずbool配列に変換してからany、all関数を実行する。

test = {
    'inoue':73,
    'suzuki':65,
    'tanaka':80,
    'yamada':51,
}

any([v >= 60 for v in test.values()])
# True

all([v >= 60 for v in test.values()])
# False

配列要素を同じ値で増やす

item_ids = [24] * 5

print item_ids
# [24, 24, 24, 24, 24]

値が挿入されるindex値を調べる

ソートされている配列に新しい要素値を挿入すると、どこの位置(index)に入るかを調べるにはbisect関数を使用する。
2分探索で調べるので高速。

import bisect

scores = [5, 10, 50, 100]
new_score = 55

index = bisect.bisect(scores, new_score)
print index
# 3

実際に要素を入れ込む場合はbisect.insort関数を使う。

import bisect

scores = [5, 10, 50, 100]
new_score = 55

bisect.insort(scores, new_score)
print scores
# [5, 10, 50, 55, 100]

-Python

関連記事

no image

[Python] dict(連想配列)の使い方

Pythonでの連想配列(dict)の使い方メモ。 宣言・要素の追加 配列を宣言 color_dict = { ‘red’: ‘赤色’, ‘blue’: ‘青色’, } print color_dic …

no image

[Python] 配列をソートさせる方法

配列要素値をソートさせる方法 昇順ソート sortメソッドを使って昇順ソートする。 numbers = [1, 5, 3, 2, 4] numbers.sort() print numbers # [ …

no image

[Python] 固定値ループする方法

固定値でのループ 1から10まで固定でループさせたい場合 >>> for i in xrange(1, 10 + 1): … print i 1 2 3 4 5 6 7 8 9 …

no image

[Python] 文字列から数値(数値から文字列)に変換する

文字列→数値に変換 int関数を実行する。 a = int(‘100’) b = int(‘200’) print(a + b) # 300 数値→文字列に変換 str関数を実行する。 a = str …

no image

[Python] 配列値をカンマ区切り文字列に変換する

配列値とカンマ区切り文字列を相互変換 配列→カンマ区切り文字列 配列要素の全文字列をカンマ区切りで連結したい場合、join関数を実行する。 arrays = [‘a’, ‘b’, ‘c’] str = …

プログラミング言語