字幕表 動画を再生する AI 自動生成字幕 字幕スクリプトをプリント 翻訳字幕をプリント 英語字幕をプリント (upbeat music) (アップビート・ミュージック) - [Instructor] If you've done any programming - 講師】プログラミングをしたことがある方 in other languages, such as JavaScript, を、JavaScriptなど他の言語でも使えるようにしました。 or C#, or Java, you've probably seen やC#、あるいはJavaを見たことがある人は多いでしょう。 or worked with anonymous functions. とか、匿名関数で作業していた。 Python also supports these Pythonはこれらもサポートしています。 and they are referred to as lambda functions. であり、ラムダ関数と呼ばれる。 Lambda functions can be passed as arguments ラムダ関数は引数として渡すことができる to other functions to perform some processing work, を他の機能に移し、何らかの処理作業を行う。 much like a callback function in a language like JavaScript. JavaScriptなどのコールバック関数のようなものです。 Typically, you see these used in situations 一般的には、以下のような場面で使用されます。 where defining a whole separate function ここで、全く別の関数を定義すると would needlessly increase the complexity of the code は、コードの複雑さを不必要に増大させます。 and reduce readability. となり、可読性が低下します。 Lambdas are defined by using the keyword lambda, ラムダは、キーワード lambda を使って定義します。 followed by any arguments that the lambda function takes の後に、ラムダ関数が受け取る引数が続きます。 and then followed by an expression. という表現が続きます。 Let's look at how they are used in practice, 実際にどのように使われているのかを見てみましょう。 because that's usually the best way to understand something. というのも、何かを理解するためには、たいていそれが一番良い方法だからです。 Here in VS Code, at the top of the file, ここVSコードでは、ファイルの先頭で I have two regular functions, 私は2つの定期的な機能をもっています。 each of which performs a conversion それぞれ、変換を行う from one temperature scale to another. を、ある温度スケールから別の温度スケールに変更することができます。 This one converts Celsius temperatures to Fahrenheit これは摂氏の温度を華氏に変換するものです。 and this one does the opposite. と、こちらはその逆をやっています。 In the main function, I have two lists of temperatures; メイン関数では、2つの温度リストを持っています。 one in the Celsius scale and one in the Fahrenheit scale. 摂氏と華氏で1つずつ。 Suppose I wanted to convert each of these lists これらのリストのそれぞれを変換したいとします。 into the other temperature scale. をもう一方の温度目盛りに変換します。 To do this, I might use the map function. そのために、map関数を使うこともある。 The map function takes a function マップ関数は、関数 as the first argument and an iterable object, を最初の引数として、反復可能なオブジェクトを指定します。 like a list, as the second. を2番目として、リストのようにします。 To convert these two lists, この2つのリストを変換するには I would write something like map 私なら、mapのようなものを書きます。 and then I would call Fahrenheit to Celsius as the function. で、関数として華氏→摂氏を呼び出すことになります。 Then I would pass the ftemps list. それから、ftempsのリストをパスする。 Then to print this out, I'll put this inside a list そして、これをプリントアウトするために、リスト内に to generate the list. を使ってリストを生成します。 Then I'll just call print on the entire thing じゃあ、全部印刷にしちゃおうかなあ and I'll copy and paste that, とコピー&ペーストしておきます。 and do the same thing with Celsius to Fahrenheit で、同じことを摂氏から華氏にする and pass in the ctemps. とctempsでパスする。 Let's run what we have. 今あるものを走らせよう。 I'll go to debug view. デバッグ表示にする Remember, you can run from the Terminal command-line Terminalのコマンドラインから実行できることを覚えておいてください。 if you don't want to use VS Code. VS Codeを使用しない場合。 I'll run this. これを実行します。 You can see the results here 結果はこちらでご覧いただけます。 that each of the temperatures has been converted. 各温度が変換されたこと。 I could just reduce the complexity of my code コードの複雑さを軽減すればいいのですが by writing each of these functions これらの関数をそれぞれ書くことで as an in-line lambda, because they're pretty simple. をインラインラムダとして使うのは、かなりシンプルだからです。 Let's go back and do that. 戻ってやりましょう。 I'll clear the console. コンソールをクリアする I'm going to copy these two lines この2行をコピーする and paste them down here. をクリックして、ここに貼り付けてください。 Now I'll replace each function with a lambda equivalent. では、それぞれの関数をラムダと同等のものに置き換えてみます。 I'll put in lambda T, because each lambda function ラムダTを入れるのは、各ラムダ関数が will take a temperature as an argument. は、引数として温度を受け取ります。 Then for the Fahrenheit to Celsius case, 次に華氏→摂氏の場合。 I will copy this expression この表現をコピーします and paste it in here. をクリックして、ここに貼り付けてください。 I change that to T 'cause it's not temp anymore. もうテンプレじゃないので、Tに変えてます。 The same thing here. こちらも同じです。 I'll write lambda T, lambda Tと書きます。 and now I will get the Celsius to Fahrenheit version. と、今度は摂氏→華氏のバージョンを入手します。 I'll copy that, paste it in, コピーして貼り付けよう and change that to T. をTに変更します。 Now you can see that the results are the same. これで、結果が同じであることがおわかりいただけると思います。 In this particular case, この特殊なケースで using the lambda expression really simplifies my code ラムダ式を使うことで、コードがとてもシンプルになりました。 because I can see the calculation 計算が見えるから right where it's being used. 使用されている右側です。 Someone else who has to work with my code, 私のコードで仕事をしなければならない人がいる。 even if that's me several years from now, それが数年後の自分であっても。 doesn't have to go digging through all the source code は、すべてのソースコードに目を通す必要はありません。 to find out where the conversion functions are defined. をクリックして、変換関数が定義されている場所を確認してください。 Obviously, lambdas aren't a good fit for every scenario. もちろん、ラムダはすべてのシナリオに適しているわけではありません。 In practice, you will, of course, 実際には、もちろんそうなります。 continue to use regular functions in your programs, は、プログラムの中で通常の関数を使い続けてください。 but lambdas can help make your code more readable しかし、ラムダはコードをより読みやすくするのに役立ちます。 when defining a full function 完全な関数を定義するとき is more effort than it's worth. は、その価値以上に労力がかかります。 (upbeat music) (アップビート・ミュージック)
B1 中級 日本語 関数 華氏 変換 コード リスト コピー Pythonチュートリアル - ラムダ関数を理解する (Python Tutorial - Understanding Lambda functions) 6 1 Summer に公開 2022 年 11 月 04 日 シェア シェア 保存 報告 動画の中の単語