0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

リスト型に利用できるメソッド (Python ver.)

Posted at

##このページについて
・リストに使えるPythonのメソッドをまとめたページ
・必要であれば順次更新

##■ list.append(x)
###→リストの末尾に要素を追加する

append_examle.py
fruits = ['apple','orange']
fruits.append('banana')
print(fruits)
#['apple','orange','banana']

####リストを追加した時の挙動

append_example2.py
fruits = ['apple','orange']
fruits.append(['banana','kiwi'])
print(fruits)
#['apple','orange',['banana','kiwi']]

##■ list.count(x)
###→リストでのxの出現回数を返す

count_example.py
number = [2, 0, 1, 1, 8, 1, 9, 3, 6, 7, 5, 1]
x = number.count(1)
print(x)
# 4

##■ list.extend(x)
###→全ての要素を対象のリストの末尾に追加

extend_example.py
number = [1, 3, 5]
number.extend([7,9,11])
print(number)
#[1,3,5,7,9,11]

##■ list.index(x[, start[,end]])
###→リストの要素を検索する

index_example.py
country = ['Japan', 'America', 'Australia', 'Korea']
country.index('Japan')
# 0
index_example2.py
number = [0,1,2,4,1,1,0,2]
number.index(0, 2, 7)
# 6

##■ list.insert(i,x)
###→指定した位置(i)に要素(x)を挿入する

insert_example.py
color = ['Blue','Green','Red']
color.insert(2,'Yellow')
print(color)
#['Blue', 'Green', 'Yellow', 'Red']

##■ list.pop(i)
###→リストの中の指定された位置(i)にある要素をリストから削除する

pop_example.py
sushi = ['maguro', 'ika', 'sa-mon', 'ikura']
x = sushi.pop(0)
print(x)
#['ika','sa-mon','ikura']

##■ list.remove(x)
###→リストの中からxと同じ要素を取り除く

remove_example.py
animals = ['dog', 'cat', 'rabbit']
animals.remove('dog')
print(animals)
#['cat', 'rabbit']

####複数削除はしてくれないみたい

remove_example2.py
animals = ['dog', 'cat', 'rabbit', 'dog']
animals.remove('dog')
print(animals)
#['cat', 'rabbit', 'dog']

##■ list.reverse()
###→リストの並び順を反転する

reverse_example.py
alphabet = ['a','b','c','d','e']
alphabet.reverse()
print(alphabet)
# ['e','d','c','b','a']
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?