2016年12月9日金曜日

Tips的な何か

この記事はAdventCalendar2016 9日目の記事です(嘘). skyです.3回目です.何回登場するんだろうね. 今回は課題チェックで3年生のPythonを見ててこんな風に書けるよって思ったことでも書いていきます. 3系中心のお話です. ## 出力を改行させない 出力を改行させたくない時はendを使います. ```python print("hoge", end="") print("fuga") print("foo") ``` 実行すると ``` hogehuga foo ``` となります. ## リストを1つの文字列にしたい ["2016", "12", "09"]というリストを連結して2016-12-09としたい時とか ```python l = ["2016", "12", "09"] print("-".join(l)) ``` 実行すると ``` 2016-12-09 ``` こんな感じ ## リストの中身と数を出したい ["a", "b", "c"]というリストで順番も欲しい時 count変数を用意してインクリメントしたいけど使わなくても出来る ```python l = ["a", "b", "c"] for i, v in enumerate(l): print(i+1, v) ``` こんな感じ ``` 1 a 2 b 3 c ``` ## パディングする n桁の数字にしたい時とか(上の例で001とかにしたい時) ```python l = ["a", "b", "c"] for i, v in enumerate(l): print(str(i+1).zfill(3), v) ``` 今回は3桁にしてみる. ``` 001 a 002 b 003 c ``` ## 文字列を整形する 文字列連結でも良いけどformat関数を使うと綺麗になったりする ```python h = 01 m = 12 s = 30 time = "{hour}:{minutes}:{second}".format(hour=h, minutes=m, second=s) print(time) ``` ``` 01:12:30 ``` {}で囲まれた部分は順番, 出現回数は問わずに使うことが出来る ## 引数のデフォルト値 引数には予めデフォルト値を設定しておくことが出来る ```python def hoge(a=100): print(a) hoge(5) hoge() ``` 2つ目の関数は引数がないが実行出来る ``` 5 100 ``` ## 可変長引数 その1 引数にはいくつでも引数を取ることの出来る可変長引数がある ```python def hoge(*args): print(args) print(sum(args)) hoge(1,2,3) ``` hoge関数にはいくつでも引数を渡すことが出来る 実行するとこんな感じ ``` (1, 2, 3) 6 ``` タプルで引数が渡されていることがわかる ## 可変長引数 その2 可変長引数は便利だがキーワード引数を使って引数が渡された時に何が渡されたかを知りたくなることがあると思う そんな時は引数を**argsとする ```python def hoge(**args): print(args) print(args.get("a", None)) print(args.get("b", None)) print(args.get("c", None)) hoge(a=1,b=2) ``` get関数を使うことでデフォルト値を設定することが出来る ``` {'b': 2, 'a': 1} 1 2 None ``` cは今回ないので第2引数の値に定義されているのがわかる ## リストの中身の型を変更したい ["1", "2", "3"]を全てint型にしたい時とか ```python l = ["1", "2", "3"] m1 = map(int, l) # or m2 = [int(x) for x in l] ``` 前者はmap関数を使用した場合 スッキリ書けるが3系からmap型になるためリスト型としてスライスとかしようとするとエラーが起きる. リストとして使いたい時は ```python m1 = list(map(int, l)) ``` とする必要がある. 後者はリスト内包表記といってPython独自の文法 リストとして使えるのでこっちの方が場合によっては便利かもしれない 以下と同じ意味 ```python tmp = [] for i in l: tmp.append(int(i)) ``` パフォーマンス面と可読性の面からリスト内包表記に慣れた方が良いです. 内包表記の中に内包表記書き出すと黒魔術化していくので適宜にしましょう. ~~需要あるならワンライナーネタも書きたいとは思うけど多分ない.~~ ## まとめ reduceとかfilterとかも調べてみると良いと思います. 組み込み関数には便利なものが多数あるのでドキュメントに一度は目を通しましょう.

5 件のコメント:

  1. TaxAct login is an online tax prep company where you can prepare and file your tax return yourself.

    返信削除
  2. norton web security is generally utilized antivirus gives the simplest to utilize and most intutive assurance for your PC and your mobiles . norton.com/setup introduce it and disregard viruses,spyware,root-units, hackers.for more subtleties visit:norton.com/setup In the Norton Setup window, tap . Under Download Norton Security from Google Play, tap . norton.com/setup Tap , and then tap to install the app After you buy Norton Antivirus visit norton.com/setup sign in to norton account then enter norton product for Norton Setup or norton.com/setup Install Norton Antivirus Presently your norton retail card has been reclaimed and you can introduce the norton item and run the setup. norton.com/setup
    Firstly go to download option and follow the directions. norton.com/setup
    will provide you easy and simple way for download, install and activation process. norton.com/setup norton web security is generally utilized antivirus gives the simplest to utilize and most intutive assurance for your PC and your mobiles .

    返信削除

  3. On the off chance that you need to reinstall office or fix it, office.com/setup you can reverify your item key on setup page and can download more established form of office. office.com/setup Download and introduce your Office applications on your work area for your requirement. office.com/setup
    Incorporates a free preliminary of Word, Excel, PowerPoint, Outlook, and more Install Office on your cell phone, and set up Outlook to work with your new Office 365 letter drop. office setup is the best programming which is broadly utilized in globe .office.com/setup It is an amazing administration that causes you release your best thoughts, complete things, and remain associated on the go.for more subtleties visit: office.com/setup today. office setup is the best software which is widely used in globe .office.com/setup It is a powerful service that helps you unleash your best ideas , get things done, and stay connected on the go.for more details visit: office.com/setup On the off chance that you need to reinstall office or fix it, you can reverify your item key on setup page and can download more established form of office.

    返信削除
  4. McAfee security programming offers a standout mcafee.com/activate amongst the best malware insurance among the various security suites in the worldwide market. mcafee.com/activate As per the most recent reports, there has been noteworthy development in digital assaults in the course of recent years. mcafee.com/activate McAfee Activate is a certain shot way you can deal with the issues with the initiation procedure. mcafee.com/activate
    Our McAfee group endeavors to acquire the best tech help for your benefit. mcafee.com/activate mcafee antivirus is widely used aantivirus helps to detect and neutralize computer virus, the mail worms,the trojan programs, mcafee.com/activate
    and also helps your system free of virus and other malware is quite a daily challenge. for more details visit here. mcafee.com/activate McAfee helps organizations orchestrate a truly integrated cyber environment that simultaneously protects, identifies and enhances security threats.

    返信削除

  5. Go to and create your Amazon account, if you already have an account then sign in to your account. amazon.com/mytv To watch online prime videos on devices like computers, laptops, smartphones, and smart tv. amazon.com/mytv You need to enter the 6 digit amazon activation code at or to. Amazon Prime Video is one of the most popular video-streaming services that contain an immense collection of popular movies. amazon.com/mytv It includes not just movies, but TV shows too. To watch your favorite TV shows and movies on Amazon Prime Video, one needs to create an Amazon Prime Video account. amazon.com/mytv Visit the website to ease the process.This gives you unlimited access to tons of movies and TV shows, both for yourself and your loved ones. amazon.com/mytv the activation process is so simple and straightforward that everyone can activate Amazon Prime Video on On your mobile device or computer, go to Sign in to your Amazon account. amazon.com/mytv Enter the code shown on your TV in to the Amazon website. Amazon Prime Video is a very similar video component as are services like Netflix and Hulu. To watch online prime videos on devices like computers, laptops, smartphones, and smart tv. amazon.com/mytv You need to enter the 6 digit amazon activation code at or to. Amazon Prime Video is one of the most popular video-streaming services that contain an immense collection of popular movies. amazon.com/mytv It includes not just movies, but TV shows too. is link to Sign in and Add Prime Video to your favorite device such as Smart TV, Laptop, Mobile, Tablet etc. amazon.com/mytv With this official link you can manage your Amazon Prime Video Subscription.

    返信削除