Share
Embed
Python Library
下記で取り扱ったPythonの組み込みライブラリのtype・dir・helpについての記述を公開します。
・Python活用 虎の巻
https://lib-arts.booth.pm/items/6019295
どれも用いることで作業の効率化が可能なので、抑えておくと良いと思います。
LiberalArts
September 07, 2024
Transcript
8 slides
-
ୈ 1 ষ ͓ͬͯ͘ͱྑ͍ΈࠐΈؔ ͱඪ४ϥΠϒϥϦ ୈ 1 ষͰ͓͑ͯ͘ͱྑ͍ΈࠐΈؔͱඪ४ϥΠϒϥϦʹ͍ͭ ͯऔΓѻ͍·͢ɻ1.1 અͰΈࠐΈؔɺ1.2
અͰඪ४ϥΠϒϥϦʹ ͍ͭͯͦΕͧΕऔΓѻ͍·͢ɻ 1.1 ͓ͬͯ͘ͱྑ͍ΈࠐΈؔ 1.1.1 ΦϒδΣΫτͷதΛѲ͢Δ Python ΦϒδΣΫτࢦʹج͍ͮͯߏ͞ΕͨϓϩάϥϛϯάݴޠͰ ͋ΓɺجຊతʹΦϒδΣΫτΛੜ͠ɺॲཧΛߦ͍·͢ɻ ʮΦϒδΣΫτͷ ੜʯΫϥεΛ໌ࣔతʹ࣮ߦͨ͠ͱ͖ͷΈͳΒͣɺ୯ʹมྻΛੜ ͨ͠ͱ͖ʹߦΘΕ͍ͯΔ͜ͱཧղ͓ͯ͘͠ͱྑ͍Ͱ͢ɻ ͜ͷΑ͏ʹੜ͞ΕΔ Python ͷʮΦϒδΣΫτʯɺଟػೳ͔ͭࣗ༝ ͕ߴ͍ҰํͰɺෳͷϥΠϒϥϦΛಉ࣌ʹ༻͢ΔࡍͳͲʹೖग़ྗͷ༷Λ ਖ਼֬ʹѲ͢Δඞཁ͕͋Γ·͢ɻ 1.1.1 ߲Ͱੜ͞Εͨ Python ͷΦϒδΣΫτʹ͍ͭͯૉૣ͘Δʹ͋ 11 -
ୈ 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 -
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 -
ୈ 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 -
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 -
ୈ 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 -
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 -
ୈ 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