計算機科学のブログ

ほしい物リスト

Python - List of Files: Functions, Modules and Files - remove method

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 4.(List of Files: Functions, Modules & Files)、EXERCISE(226/682)の解答を求めてみる。

Jupyter(コード、入出力結果)

Files.ipynb

import os

from swimclub import FOLDER


swim_files = os.listdir(FOLDER)
swim_files
['Hannah-13-100m-Free.txt',
 'Darius-13-100m-Back.txt',
 'Owen-15-100m-Free.txt',
 'Mike-15-100m-Free.txt',
 'Hannah-13-100m-Back.txt',
 'Mike-15-100m-Back.txt',
 'Mike-15-100m-Fly.txt',
 'Abi-10-50m-Back.txt',
 'Ruth-13-200m-Free.txt',
 '.DS_Store',
 'Tasmin-15-100m-Back.txt',
 'Erika-15-100m-Free.txt',
 'Ruth-13-200m-Back.txt',
 'Abi-10-50m-Free.txt',
 'Maria-9-50m-Free.txt',
 'Elba-14-100m-Free.txt',
 'Tasmin-15-100m-Free.txt',
 'Abi-10-100m-Back.txt',
 'Abi-10-50m-Breast.txt',
 'Mike-15-200m-IM.txt',
 'Ali-12-100m-Back.txt',
 'Ruth-13-100m-Back.txt',
 'Chris-17-100m-Back.txt',
 'Ali-12-100m-Free.txt',
 'Darius-13-100m-Breast.txt',
 'Ruth-13-100m-Free.txt',
 'Aurora-13-50m-Free.txt',
 'Katie-9-100m-Breast.txt',
 'Alison-14-100m-Breast.txt',
 'Ruth-13-400m-Free.txt',
 'Emma-13-100m-Free.txt',
 'Calvin-9-50m-Fly.txt',
 'Darius-13-100m-Fly.txt',
 'Mike-15-200m-Free.txt',
 'Emma-13-100m-Breast.txt',
 'Tasmin-15-100m-Breast.txt',
 'Blake-15-100m-Free.txt',
 'Abi-10-100m-Breast.txt',
 'Chris-17-100m-Breast.txt',
 'Blake-15-100m-Back.txt',
 'Bill-18-200m-Back.txt',
 'Darius-13-200m-IM.txt',
 'Dave-17-100m-Free.txt',
 'Alison-14-100m-Free.txt',
 'Lizzie-14-100m-Free.txt',
 'Katie-9-50m-Fly.txt',
 'Katie-9-50m-Breast.txt',
 'Katie-9-50m-Back.txt',
 'Lizzie-14-100m-Back.txt',
 'Tasmin-15-200m-Breast.txt',
 'Katie-9-50m-Free.txt',
 'Dave-17-200m-Back.txt',
 'Erika-15-200m-Breast.txt',
 'Calvin-9-50m-Back.txt',
 'Calvin-9-50m-Free.txt',
 'Carl-15-100m-Back.txt',
 'Bill-18-100m-Back.txt',
 'Katie-9-100m-Free.txt',
 'Blake-15-100m-Fly.txt',
 'Erika-15-100m-Breast.txt',
 'Katie-9-100m-Back.txt']
sorted(swim_files)
['.DS_Store',
 'Abi-10-100m-Back.txt',
 'Abi-10-100m-Breast.txt',
 'Abi-10-50m-Back.txt',
 'Abi-10-50m-Breast.txt',
 'Abi-10-50m-Free.txt',
 'Ali-12-100m-Back.txt',
 'Ali-12-100m-Free.txt',
 'Alison-14-100m-Breast.txt',
 'Alison-14-100m-Free.txt',
 'Aurora-13-50m-Free.txt',
 'Bill-18-100m-Back.txt',
 'Bill-18-200m-Back.txt',
 'Blake-15-100m-Back.txt',
 'Blake-15-100m-Fly.txt',
 'Blake-15-100m-Free.txt',
 'Calvin-9-50m-Back.txt',
 'Calvin-9-50m-Fly.txt',
 'Calvin-9-50m-Free.txt',
 'Carl-15-100m-Back.txt',
 'Chris-17-100m-Back.txt',
 'Chris-17-100m-Breast.txt',
 'Darius-13-100m-Back.txt',
 'Darius-13-100m-Breast.txt',
 'Darius-13-100m-Fly.txt',
 'Darius-13-200m-IM.txt',
 'Dave-17-100m-Free.txt',
 'Dave-17-200m-Back.txt',
 'Elba-14-100m-Free.txt',
 'Emma-13-100m-Breast.txt',
 'Emma-13-100m-Free.txt',
 'Erika-15-100m-Breast.txt',
 'Erika-15-100m-Free.txt',
 'Erika-15-200m-Breast.txt',
 'Hannah-13-100m-Back.txt',
 'Hannah-13-100m-Free.txt',
 'Katie-9-100m-Back.txt',
 'Katie-9-100m-Breast.txt',
 'Katie-9-100m-Free.txt',
 'Katie-9-50m-Back.txt',
 'Katie-9-50m-Breast.txt',
 'Katie-9-50m-Fly.txt',
 'Katie-9-50m-Free.txt',
 'Lizzie-14-100m-Back.txt',
 'Lizzie-14-100m-Free.txt',
 'Maria-9-50m-Free.txt',
 'Mike-15-100m-Back.txt',
 'Mike-15-100m-Fly.txt',
 'Mike-15-100m-Free.txt',
 'Mike-15-200m-Free.txt',
 'Mike-15-200m-IM.txt',
 'Owen-15-100m-Free.txt',
 'Ruth-13-100m-Back.txt',
 'Ruth-13-100m-Free.txt',
 'Ruth-13-200m-Back.txt',
 'Ruth-13-200m-Free.txt',
 'Ruth-13-400m-Free.txt',
 'Tasmin-15-100m-Back.txt',
 'Tasmin-15-100m-Breast.txt',
 'Tasmin-15-100m-Free.txt',
 'Tasmin-15-200m-Breast.txt']
help(swim_files)
Help on list object:

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 bool(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__(self, index, /)
 |      Return self[index].
 |
 |  __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)
 |      Sort the list in ascending order and return None.
 |
 |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
 |      order of two equal elements is maintained).
 |
 |      If a key function is given, apply it once to each list item and sort them,
 |      ascending or descending, according to their function values.
 |
 |      The reverse flag can be set to sort in descending order.
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  __class_getitem__(object, /)
 |      See PEP 585
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs)
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  __hash__ = None
dir(swim_files)
['__add__',
 '__class__',
 '__class_getitem__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'clear',
 'copy',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']
help(swim_files.remove)
Help on built-in function remove:

remove(value, /) method of builtins.list instance
    Remove first occurrence of value.

    Raises ValueError if the value is not present.
len(swim_files)
61
swim_files.remove('.DS_Store')
len(swim_files)
60
sorted(swim_files)[0]
'Abi-10-100m-Back.txt'