LoginSignup
0
0

More than 3 years have passed since last update.

【Udemy Python3入門+応用】 18.リストのメソッド

Posted at

※この記事はUdemyの
現役シリコンバレーエンジニアが教えるPython3入門+応用+アメリカのシリコンバレー流コードスタイル
の講座を受講した上での、自分用の授業ノートです。
講師の酒井潤さんから許可をいただいた上で公開しています。

■リストのメソッド

.index()
.index
r = [1, 2, 3, 4, 5, 1, 2, 3]
print(r.index(3))
result
2

.index()を使うことで、指定した要素のインデックスを調べることができる。
この場合、先頭から探してくれるので、インデックスが2と返ってきている(1番目の「3」を検出している)。

.index
r = [1, 2, 3, 4, 5, 1, 2, 3]
print(r.index(3, 3))
result
7

.index(3, 3)
最初の3は「『3』を探してくれ」というように、検索する要素を指定しており、
後ろの3は「インデックス『3』以降から探してくれ」というように、検索範囲を指定している。

.count()
.count
r = [1, 2, 3, 4, 5, 1, 2, 3]
print(r.count(3))
result
2

「『3』が何個あるか数えておくれ」という意味。

◆if文
if
r = [1, 2, 3, 4, 5, 1, 2, 3]

if 5 in r:
    print('exist')
result
exist

If '5' exists in r, print "exist."
という意味。
if文については後の授業で扱う。

◆数字順に並べる
sort_and_reverse
r = [1, 2, 3, 4, 5, 1, 2, 3]

r.sort()
print(r)

r.sort(reverse=True)
print(r)

r.reverse()
print(r)
result
[1, 1, 2, 2, 3, 3, 4, 5]
[5, 4, 3, 3, 2, 2, 1, 1]
[1, 1, 2, 2, 3, 3, 4, 5]

.sort()を用いることで、数字の小さい順にリストの要素を並べ替えできる。
.sort(reverse=True)とすると、逆順にできる。
.reverse()というメソッドも用意されているので、それでもOK。

.split().join()
split_and_join
s = 'My name is Tony.'

to_split = s.split(' ')
print(to_split)

x = ' '.join(to_split)
print(x)
result
['My', 'name', 'is', 'Tony.']
My name is Tony.

.split(' ')によって、
(スペース)で文字列をバラバラにして、それを要素にしてリストを作ってくれ」
ということができる。
逆に、' '.join()を使うことで、
(スペース)を要素間に挟んで、要素を結合して文字列にしてくれ」
ということができる。

◆help
help
help(list)
result
class list(object)
 |  list(iterable=(), /)
 |  
 |  Built-in mutable sequence.
 |  
 |  If no argument is given, the constructor creates a new empty list.
 |  The argument must be an iterable if specified.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iadd__(self, value, /)
 |      Implement self+=value.
 |  
 |  __imul__(self, value, /)
 |      Implement self*=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(self, /)
 |      Return a reverse iterator over the list.
 |  
 |  __rmul__(self, value, /)
 |      Return value*self.
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(self, /)
 |      Return the size of the list in memory, in bytes.
 |  
 |  append(self, object, /)
 |      Append object to the end of the list.
 |  
 |  clear(self, /)
 |      Remove all items from list.
 |  
 |  copy(self, /)
 |      Return a shallow copy of the list.
 |  
 |  count(self, value, /)
 |      Return number of occurrences of value.
 |  
 |  extend(self, iterable, /)
 |      Extend list by appending elements from the iterable.
 |  
 |  index(self, value, start=0, stop=9223372036854775807, /)
 |      Return first index of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  insert(self, index, object, /)
 |      Insert object before index.
 |  
 |  pop(self, index=-1, /)
 |      Remove and return item at index (default last).
 |      
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(self, value, /)
 |      Remove first occurrence of value.
 |      
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(self, /)
 |      Reverse *IN PLACE*.
 |  
 |  sort(self, /, *, key=None, reverse=False)
 |      Stable sort *IN PLACE*.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None

helpでメソッドの使い方を呼び出すこともできる。

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