Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Library

LiberalArts
September 07, 2024
130

Python Library

下記で取り扱ったPythonの組み込みライブラリのtype・dir・helpについての記述を公開します。

・Python活用 虎の巻
https://lib-arts.booth.pm/items/6019295

どれも用いることで作業の効率化が可能なので、抑えておくと良いと思います。

LiberalArts

September 07, 2024
Tweet

Transcript

  1. ୈ 1 ষ ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ ਺ͱඪ४ϥΠϒϥϦ ୈ 1 ষͰ͸཈͓͑ͯ͘ͱྑ͍૊ΈࠐΈؔ਺ͱඪ४ϥΠϒϥϦʹ͍ͭ ͯऔΓѻ͍·͢ɻ1.1 અͰ૊ΈࠐΈؔ਺ɺ1.2

    અͰඪ४ϥΠϒϥϦʹ ͍ͭͯͦΕͧΕऔΓѻ͍·͢ɻ 1.1 ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ 1.1.1 ΦϒδΣΫτͷத਎Λ೺Ѳ͢Δ Python ͸ΦϒδΣΫτࢦ޲ʹج͍ͮͯߏ੒͞ΕͨϓϩάϥϛϯάݴޠͰ ͋ΓɺجຊతʹΦϒδΣΫτΛੜ੒͠ɺॲཧΛߦ͍·͢ɻ ʮΦϒδΣΫτͷ ੜ੒ʯ͸ΫϥεΛ໌ࣔతʹ࣮ߦͨ͠ͱ͖ͷΈͳΒͣɺ୯ʹม਺΍഑ྻΛੜ੒ ͨ͠ͱ͖ʹ΋ߦΘΕ͍ͯΔ͜ͱ͸ཧղ͓ͯ͘͠ͱྑ͍Ͱ͢ɻ ͜ͷΑ͏ʹੜ੒͞ΕΔ Python ͷʮΦϒδΣΫτʯ͸ɺଟػೳ͔ͭࣗ༝౓ ͕ߴ͍ҰํͰɺෳ਺ͷϥΠϒϥϦΛಉ࣌ʹ࢖༻͢ΔࡍͳͲʹೖग़ྗͷ࢓༷Λ ਖ਼֬ʹ೺Ѳ͢Δඞཁ͕͋Γ·͢ɻ 1.1.1 ߲Ͱ͸ੜ੒͞Εͨ Python ͷΦϒδΣΫτʹ͍ͭͯૉૣ͘஌Δʹ͋ 11
  2. ୈ 1 ষ ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ͱඪ४ϥΠϒϥϦ ͨͬͯ༗༻ͳؔ਺ʹ͍ͭͯ֬ೝ͠·͢ɻ۩ମతʹ͸ type ؔ਺ɺdir ؔ਺ɺh elp ؔ਺ͳͲʹ͍ͭͯ཈͓͑ͯ͘ͱྑ͍ͷͰɺҎԼͦΕͧΕͷؔ਺ʹ͍ͭͯ

    ֬ೝ͠·͢ɻ type ؔ਺ type ؔ਺Λ༻͍Δ͜ͱͰΦϒδΣΫτͷܗࣜʹ͍ͭͯ஌Δ͜ͱ͕Ͱ͖· ͢ɻͨͱ͑͹ɺϓϩάϥϜ 1.1 Λ࣮ߦ͢Δ͜ͱͰɺͦΕͧΕͷม਺ͷܕΛ֬ ೝͰ͖·͢ɻ ˛ϓϩάϥϜ 1.1 type ؔ਺ᶃ 1 x1 = 1 2 x2 = 1.1 3 y = "abc" 4 5 print(type(x1)) 6 print(type(x2)) 7 print(type(y)) ˛ ࣮ߦ݁Ռ <class 'int '> <class 'float '> <class 'str '> ࣮ߦ݁ՌΑΓɺ੔਺Λ୅ೖͨ͠ x1 ͸ int ܕɺখ਺Λ୅ೖͨ͠ x2 ͸ floa t ܕɺจࣈྻΛ୅ೖͨ͠ y ͸ str ܕͰ͋Δ͜ͱ͕ͦΕͧΕ֬ೝͰ͖·͢ɻ type ؔ਺͸೚ҙͷΦϒδΣΫτʹ͍࣮ͭͯߦͰ͖ΔͷͰɺϓϩάϥϜ 1.1 ͷΑ͏ʹม਺͚ͩͰͳ͘ϓϩάϥϜ 1.2 ͷΑ͏ʹ഑ྻʹ͍ͭͯ΋֬ೝͰ͖ ·͢ɻ ˛ϓϩάϥϜ 1.2 type ؔ਺ᶄ 1 z1 = [1, 2, 3] 2 z2 = (1, 2, 3) 3 z3 = {"a1":1, "a2":3} 4 5 print(type(z1)) 6 print(type(z2)) 7 print(type(z3)) 12
  3. 1.1 ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ ˛ ࣮ߦ݁Ռ <class 'list '> <class 'tuple '>

    <class 'dict '> Python Ͱ഑ྻΛऔΓѻ͏ʹ͋ͨͬͯ͸ɺओʹ list ܕɺtuple ܕɺdic t ܕͷࡾछྨ͋ΓɺϓϩάϥϜ 1.2 Ͱ͸ͦΕͧΕʹ͍ͭͯ֬ೝ͠·ͨ͠ɻ· ͨɺtype ؔ਺͸ϓϩάϥϜ 1.3 ͷΑ͏ʹ֎෦ϥΠϒϥϦʹ͍ͭͯ΋࣮ߦͰ ͖·͢ɻ ˛ϓϩάϥϜ 1.3 type ؔ਺ᶅ 1 import numpy as np 2 3 x = np.array ([1, 2]) 4 5 print(type(np)) 6 print(type(np.array)) 7 print(type(x)) ˛ ࣮ߦ݁Ռ <class 'module '> <class 'builtin_function_or_method '> <class 'numpy.ndarray '> ϓϩάϥϜ 1.3 Ͱ͸ Python ͰߦྻԋࢉΛߦ͏ࡍʹΑ͘༻͍ΒΕΔ NumP y ͷΦϒδΣΫτʹ͍͍͔ͭͯͭ֬͘ೝΛߦ͍·ͨ͠ɻNumPy ʹ͍ͭͯ͸ 2.1.1 ߲Ͱৄ͘͠औΓѻ͍·͢ɻ dir ؔ਺ dir ؔ਺Λ༻͍Δ͜ͱͰ Python ͷΦϒδΣΫτͷʮΠϯελϯεม਺ʯ ͱʮϝιουʯͷҰཡΛऔಘ͢Δ͜ͱ͕Ͱ͖·͢ɻϓϩάϥϜ 1.4 ͷΑ͏ʹ dir ؔ਺Λ༻͍Δ͜ͱͰʮΠϯελϯεม਺ʯͱʮϝιουʯͷҰཡͷऔಘ ͕ՄೳͰ͢ɻ ˛ϓϩάϥϜ 1.4 dir ؔ਺ᶃ 1 x = 100 2 13
  4. ୈ 1 ষ ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ͱඪ४ϥΠϒϥϦ 3 print(type(x)) 4 print("===") 5 print(dir(x))

    6 print("===") 7 print(x.real) 8 print(x.imag) 9 print(x.conjugate ()) ˛ ࣮ߦ݁Ռ <class 'int '> === ['__abs__ ', '__add__ ', '__and__ ', '__bool__ ', '__ceil__ ', '__class__ ', '__delattr__ ', '__dir__ ', '__divmod__ ', '__doc__ ', '__eq__ ', ' __float__ ', '__floor__ ', '__floordiv__ ', '__format__ ', '__ge__ ', ' __getattribute__ ', '__getnewargs__ ', '__getstate__ ', '__gt__ ', ' __hash__ ', '__index__ ', '__init__ ', '__init_subclass__ ', '__int__ ', ' __invert__ ', '__le__ ', '__lshift__ ', '__lt__ ', '__mod__ ', '__mul__ ', ' __ne__ ', '__neg__ ', '__new__ ', '__or__ ', '__pos__ ', '__pow__ ', ' __radd__ ', '__rand__ ', '__rdivmod__ ', '__reduce__ ', '__reduce_ex__ ', ' __repr__ ', '__rfloordiv__ ', '__rlshift__ ', '__rmod__ ', '__rmul__ ', ' __ror__ ', '__round__ ', '__rpow__ ', '__rrshift__ ', '__rshift__ ', ' __rsub__ ', '__rtruediv__ ', '__rxor__ ', '__setattr__ ', '__sizeof__ ', ' __str__ ', '__sub__ ', '__subclasshook__ ', '__truediv__ ', '__trunc__ ', ' __xor__ ', 'as_integer_ratio ', 'bit_count ', 'bit_length ', 'conjugate ', 'denominator ', 'from_bytes ', 'imag ', 'numerator ', 'real ', 'to_bytes '] === 100 0 100 ϓϩάϥϜ 1.4 Ͱ͸ int ܕͷม਺Λද͢ΦϒδΣΫτͷʮΠϯελϯεม ਺ʯͱʮϝιουʯͷҰཡΛऔಘ͠ɺrealɾimag ͱɺconjugate ϝιου ͷத਎ͷग़ྗΛߦ͍·ͨ͠ɻ ·ͨɺint ܕͷΦϒδΣΫτ͸ bit_countɾbit_length ͷΑ͏ͳϝιο υΛ࣋ͪ·͢ɻͦΕͧΕͷϝιου͸ϓϩάϥϜ 1.5 ͷΑ͏ʹ࣮ߦ͢Δ͜ͱ ͕Ͱ͖·͢ɻ ˛ϓϩάϥϜ 1.5 dir ؔ਺ᶄ 1 print(bin(x)) 2 print(x.bit_count ()) 3 print(x.bit_length ()) ˛ ࣮ߦ݁Ռ 0b1100100 14
  5. 1.1 ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ 3 7 bin ؔ਺Λ༻͍Δ͜ͱͰɺ੔਺ΛϏοτܗࣜʹม׵͢Δ͜ͱ͕Ͱ͖·͢ɻ 0b1100100 ͸ 100 =

    26 + 25 + 22 ͷΑ͏ʹཧղ͢Δͱྑ͍Ͱ͢ɻbit_cou nt ͸ 1 ͷ਺ɺbit_length ͸Ϗοτͷ௕͞ΛͦΕͧΕऔಘ͠·͢ɻ ͜͜Ͱ realɾimag ͸ΦϒδΣΫτͷΠϯελϯεม਺ɺconjugateɾb it_countɾbit_length ͸ΦϒδΣΫτͷϝιουͰ͢ɻ͜ͷΑ͏ʹ dir ؔ਺Ͱ͸ΦϒδΣΫτͷΠϯελϯεม਺ͱϝιουͷݟ෼͚͕͔ͭͳ͍͜ ͱʹ஫ҙ͕ඞཁͰ͢ɻ͜ͷ൑ผʹ͋ͨͬͯ͸ɺϓϩάϥϜ 1.6 ͷΑ͏ʹ૊Έ ࠐΈؔ਺ͷ getattr ͱ callable Λ༻͍Δ͜ͱͰɺରॲ͕ՄೳͰ͢*1 ɻ ˛ϓϩάϥϜ 1.6 dir ؔ਺ᶅ 1 instance_values = [value for value in dir(x) if not callable(getattr(x , value))] 2 methods = [method for method in dir(x) if callable(getattr(x, method)) ] 3 4 print( instance_values ) 5 print("=====") 6 print(methods) ˛ ࣮ߦ݁Ռ ['__doc__ ', 'denominator ', 'imag ', 'numerator ', 'real '] ===== ['__abs__ ', '__add__ ', '__and__ ', '__bool__ ', '__ceil__ ', '__class__ ', '__delattr__ ', '__dir__ ', '__divmod__ ', '__eq__ ', '__float__ ', ' __floor__ ', '__floordiv__ ', '__format__ ', '__ge__ ', '__getattribute__ ' , '__getnewargs__ ', '__getstate__ ', '__gt__ ', '__hash__ ', '__index__ ', '__init__ ', '__init_subclass__ ', '__int__ ', '__invert__ ', '__le__ ', ' __lshift__ ', '__lt__ ', '__mod__ ', '__mul__ ', '__ne__ ', '__neg__ ', ' __new__ ', '__or__ ', '__pos__ ', '__pow__ ', '__radd__ ', '__rand__ ', ' __rdivmod__ ', '__reduce__ ', '__reduce_ex__ ', '__repr__ ', ' __rfloordiv__ ', '__rlshift__ ', '__rmod__ ', '__rmul__ ', '__ror__ ', ' __round__ ', '__rpow__ ', '__rrshift__ ', '__rshift__ ', '__rsub__ ', ' __rtruediv__ ', '__rxor__ ', '__setattr__ ', '__sizeof__ ', '__str__ ', ' __sub__ ', '__subclasshook__ ', '__truediv__ ', '__trunc__ ', '__xor__ ', ' as_integer_ratio ', 'bit_count ', 'bit_length ', 'conjugate ', 'from_bytes ', 'to_bytes '] *1 getattr ؔ਺͸ΦϒδΣΫτͱΠϯελϯεม਺ɾϝιουͷ໊শΛೖྗ͢Δ͜ͱͰɺ ΦϒδΣΫτ͔Β஋΍ϝιουΛऔΓग़ؔ͢਺ɺcallable ؔ਺͸ΦϒδΣΫτ͕ݺͼ ग़͠Մೳ͔Λ൑ఆ͢Δؔ਺Ͱ͢ɻ 15
  6. ୈ 1 ষ ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ͱඪ४ϥΠϒϥϦ ϓϩάϥϜ 1.6*2 ͷ࣮ߦ݁ՌΑΓɺΠϯελϯεม਺ͷ imagɾreal ͱɺ ϝιουͷ

    conjugateɾbit_countɾbit_length ͷ൑ผ͕ՄೳͰ͋Δ͜ ͱ͕֬ೝͰ͖·͢ɻ help ؔ਺ dir ؔ਺Λ༻͍Δ͜ͱͰΦϒδΣΫτͷʮΠϯελϯεม਺ʯ΍ʮϝιο υʯͷҰཡʹ͍ͭͯ֬ೝͰ͖ΔҰํͰɺͦΕͧΕͷ࢓༷΋ಉ࣌ʹ֬ೝͰ͖Δ ͱޮ཰తͰ͢ɻ͜ͷΑ͏ͳࡍʹ༗༻ͳͷ͕ help ؔ਺Ͱ͢ɻ ˛ϓϩάϥϜ 1.7 help ؔ਺ᶃ 1 x = 100 2 3 print(help(x.bit_length)) ˛ ࣮ߦ݁Ռ Help on built -in function bit_length: bit_length () method of builtins.int instance Number of bits necessary to represent self in binary. >>> bin (37) '0b100101 ' >>> (37).bit_length () 6 None ϓϩάϥϜ 1.7 Λ࣮ߦ͢Δ͜ͱͰɺint ܕͷϝιουͰ͋Δ bit_length ͷ֓ཁ΍࢖༻ྫΛ֬ೝ͢Δ͜ͱ͕Ͱ͖·͢ɻϓϩάϥϜ 1.8 Ͱ͸ಉ༷ʹ bit _count ʹ͍ͭͯ֬ೝΛߦ͍·͢ɻ ˛ϓϩάϥϜ 1.8 help ؔ਺ᶄ 1 print(help(x.bit_count)) *2 ͜͜Ͱ͸Ϧετ಺แදهΛ༻͍·ͨ͠ɺϦετ಺แදهʹ͍ͭͯ͸ Appendix Ͱৄ͘͠ औΓѻ͍·ͨ͠ɻ 16
  7. 1.1 ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ ˛ ࣮ߦ݁Ռ Help on built -in function bit_count:

    bit_count () method of builtins.int instance Number of ones in the binary representation of the absolute value of self. Also known as the population count. >>> bin (13) '0b1101 ' >>> (13).bit_count () 3 None help ؔ਺͸ϓϩάϥϜ 1.7 ΍ϓϩάϥϜ 1.8 ͷΑ͏ʹΦϒδΣΫτͷϝ ιου͚ͩͰ͸ͳ͘ɺΦϒδΣΫτͷ࢓༷ʹ͍ͭͯ΋֬ೝ͢Δ͜ͱ͕Ͱ͖· ͢ɻϓϩάϥϜ 1.9 Ͱ͸ int ܕͷΦϒδΣΫτͷ࢓༷ʹ͍ͭͯ֬ೝΛߦ͍ ·ͨ͠*3 ɻ ˛ϓϩάϥϜ 1.9 help ؔ਺ᶅ 1 x = 100 2 3 print(help(x)) ˛ ࣮ߦ݁Ռ Help on int object: class int(object) | int ([x]) -> integer | int(x, base =10) -> integer | | Convert a number or string to an integer , or return 0 if no arguments | are given. If x is a number , return x.__int__ (). For floating point | numbers , this truncates towards zero. (( த ུ )) | | bit_count(self , /) *3 ΦϒδΣΫτͷ࢓༷͸෼ྔ͕ଟ͘ͳΓ͕ͪͳͷͰɺجຊతʹ͸ dir ͳͲΛ༻͍ͯઌʹΦ ϒδΣΫτͷʮΠϯελϯεม਺ʯ΍ʮϝιουʯʹ͍ͭͯ೺Ѳ্ͨ͠Ͱ help ؔ਺Λ༻ ͍Δͷ͕ΑΓྑ͍ͱࢥ͍·͢ɻ 17
  8. ୈ 1 ষ ஌͓ͬͯ͘ͱྑ͍૊ΈࠐΈؔ਺ͱඪ४ϥΠϒϥϦ | Number of ones in the

    binary representation of the absolute value of self. | | Also known as the population count. | | >>> bin (13) | '0b1101 ' | >>> (13).bit_count () | 3 | | bit_length(self , /) | Number of bits necessary to represent self in binary. | | >>> bin (37) | '0b100101 ' | >>> (37).bit_length () | 6 | | conjugate (...) | Returns self , the complex conjugate of any int. (( த ུ )) | | imag | the imaginary part of a complex number | | numerator | the numerator of a rational number in lowest terms | | real | the real part of a complex number None 1.1.2 ഑ྻͱ܁Γฦ͠ list ؔ਺ list ؔ਺͸ Python ʹ͓͚Δ഑ྻΛऔΓѻ͏ܗࣜͷҰͭͰ͋ΔϦετʹ ͍ͭͯऔΓѻ͏ؔ਺Ͱ͢ɻͨͱ͑͹ϓϩάϥϜ 1.10 ͷΑ͏ͳॲཧΛ࣮ߦ͠ ·͢ɻ ˛ϓϩάϥϜ 1.10 list ؔ਺ᶃ 1 x = list ([1, 5, 3]) 2 print(x) 3 print(type(x)) ˛ ࣮ߦ݁Ռ 18