Python 配列 list

Pythonで配列 listを使用するサンプルです。

# -*- coding: Shift_JIS -*

# 数値
list = [1, 2, 3, 4, 5]

for i in list:
    print(i)

slist = ["あ", "い", "う", "え", "お"]

# 文字列
for i in slist:
    print(i)

# 要素の追加 appendを使用すると最後尾に追加
list.append(6)
list.append(7)

print("リスト追加")
for i in list:
    print(i)

# インデックスを指定して追加 任意の場所に挿入可能
list.insert(0, 0)

print(list)

# 要素の削除1 要素を指定し、最初に一致した要素のみを削除(インデックスの指定ではないです)
list2 = [1, 2, 3, 4, 5]

print("要素の削除")
list2.remove(1)
print(list2)

# 要素の削除2 インデックスを指定し、削除。削除後、戻り値として削除した要素を返します。
print("要素の削除2")
list3 = [1, 2, 3, 4, 5]
print(list3.pop(1))

# 要素数の取得
print("要素数")
list4 = [1, 2, 2, 2, 3, 4, 5]
print(len(list4))

# 並び替え
print("ソート")
list4.sort()
print(list4)

Bookmark this on Yahoo Bookmark
Bookmark this on Google Bookmarks
Share on LinkedIn
LINEで送る
Pocket

コメントを残す

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

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>