js |
JavaScriptタグが付けられた新着投稿 - Qiita |
React の Minifyについて |
https://qiita.com/omo_taku/items/c7658616d6d3be5bcbed
|
javascript |
2023-05-28 01:24:29 |
js |
JavaScriptタグが付けられた新着投稿 - Qiita |
関数型プログラミングについての考察 |
https://qiita.com/Hirohana/items/e13b3f779c461fddd15f
|
関数型プログラミング |
2023-05-28 01:18:43 |
Ruby |
Rubyタグが付けられた新着投稿 - Qiita |
StringScannerの紹介 |
https://qiita.com/okuramasafumi/items/300d0925a9c91a746455
|
string |
2023-05-28 01:51:24 |
AWS |
AWSタグが付けられた新着投稿 - Qiita |
【用語集】駆け出しエンジニアがAWS認定資格勉強中に遭遇した慣れない横文字カタカナ語まとめ【随時更新】 |
https://qiita.com/Guruguru_Brain/items/e3ea40f10982de3bcc79
|
駆け出し |
2023-05-28 01:43:05 |
技術ブログ |
Developers.IO |
CloudFormationで「Fn::Sub syntax must contain only alphanumeric characters, underscores, periods, and colons」と出た時の対処方法 |
https://dev.classmethod.jp/articles/what-to-do-when-you-get-fnsub-syntax-must-contain-only-alphanumeric-characters-underscores-periods-and-colons-in-cloudformation/
|
|
2023-05-27 16:40:20 |
海外TECH |
MakeUseOf |
5 Free Interactive Presentation Tools You Can Try Online |
https://www.makeuseof.com/free-interactive-presentation-tools/
|
programs |
2023-05-27 16:30:19 |
海外TECH |
MakeUseOf |
How to Tell When Android Apps Sneakily Access Your Camera or Microphone |
https://www.makeuseof.com/apps-access-camera-microphone/
|
indicators |
2023-05-27 16:30:19 |
海外TECH |
MakeUseOf |
How to Customize Your Outlook Calendar on Windows |
https://www.makeuseof.com/customize-outlook-calendar-windows/
|
windows |
2023-05-27 16:16:17 |
海外TECH |
DEV Community |
Sorting complex objects with custom criteria in Python |
https://dev.to/renzhamin/sorting-complex-objects-with-custom-criteria-in-python-i89
|
Sorting complex objects with custom criteria in PythonIn most cases sorting is a simple task but there are occasions when the data is a bit complicated and python provides elegant way to handle those scenarios So we will look at a complex scenario in this article Object structure With class definitionclass Laptop def init self cpu ram ssd gt None self cpu cpu self ram ram self ssd ssdA Laptop Ryzen B Laptop Ryzen C Laptop Ryzen D Laptop Ryzen arr A B C D As list itemsA Ryzen B Ryzen C Ryzen D Ryzen arr A B C D Table of ContentsSort using key parameterOperator overloadingComparator functionThe smarter way to solve this problemConclusion Sort using key parameterLets say our priority is in this order cpu gt ram gt ssd As class objectsarr sort key lambda x x cpu x ram x ssd reverse True As list itemsarr sort key lambda x x x x reverse True The result is Ryzen Ryzen Ryzen Ryzen For simpler cases when you have only one criteria you don t need to use tuplesarr sort key lambda x x cpu reverse True Operator overloadingLets make the scenario more complex by introducing Intel E Laptop Intel i arr A B C D E If we don t change anything the result will be Ryzen Ryzen Ryzen Ryzen Intel i Which may not be what we want we were getting results as expected before because we assumed that Ryzen gt Ryzen which is carried out by the lt operator of stringFor the sake of demonstration lets define the precedence in this way Ryzen gt Intel i gt RyzenHere is one way to achieve this result using operator overloadingclass Laptop def init self cpu ram ssd gt None self cpu cpu self ram ram self ssd ssd def lt a b brand a model a a cpu split brand b model b b cpu split if brand a brand b if model a model b return model a lt model b else if brand a Intel return b cpu Ryzen elif brand b Intel return a cpu Ryzen if a ram b ram return a ram lt b ram return a ssd lt b ssdIn this function returning means a is smaller if is returned then b is smallerWhen the lt operator is defined you can sort by simply callingarr sort reverse True Comparator functionfrom functools import cmp to keydef comparator a b return a ram b ramarr sort key cmp to key comparator reverse True You can write logic of similar complexity by using comparator functionBut in this method returning or any negative number means a is smaller if positive number is returned than b is smaller If is returned then they have the same priority and no swapping will be done The benefit of using comparator function is that you don t overload the class operators and you have the option to use multiple comparators for different use cases If you have already overloaded the lt operator but a scenario arises where your criteria is a bit different then comparator function is what you may need The smarter way to solve this problemUse a dictionary to define the priority of the cpu models mp Ryzen Intel i Ryzen Then the logic becomes simpler def lt a b if a cpu b cpu return mp a cpu lt mp b cpu if a ram b ram return a ram lt b ram return a ssd lt b ssdTo use comparator function just replace the less than lt operators with minus operatorYou may have already guessed that you can make it even simpler without using operator overloadingarr sort key lambda x mp x cpu x ram x ssd reverse True This will be the result Ryzen Ryzen Intel i Ryzen Ryzen ConclusionAll three ways may have their places but if you can perform the task by using lambda in key parameter than you should stick to thatFind me onGithubLinkedInTwitter |
2023-05-27 16:22:30 |
Apple |
AppleInsider - Frontpage News |
How to move Final Cut Pro projects from Mac to iPad |
https://appleinsider.com/inside/final-cut-pro/tips/how-to-move-final-cut-pro-projects-from-mac-to-ipad?utm_medium=rss
|
How to move Final Cut Pro projects from Mac to iPadIf you use Final Cut Pro on your iPad you may know you can t migrate a library from your Mac to your tablet Turns out there is a way to do it but it s not obvious Adjusting color on Final Cut Pro for iPadFinal Cut Pro for iPad has launched with the omission of one major feature ーround tripping This is where a project can move seamlessly from one device to another and back again Read more |
2023-05-27 16:50:49 |
ニュース |
BBC News - Home |
Anger over airports' passport e-gates not working |
https://www.bbc.co.uk/news/uk-65731795?at_medium=RSS&at_campaign=KARANGA
|
airports |
2023-05-27 16:46:24 |
ニュース |
BBC News - Home |
Ukraine war: Russian Ambassador Andrei Kelin issues warning of escalation in Ukraine |
https://www.bbc.co.uk/news/uk-politics-65720980?at_medium=RSS&at_campaign=KARANGA
|
weapons |
2023-05-27 16:51:09 |
ニュース |
BBC News - Home |
Premiership final: Saracens 35-25 Sale: Sarries win sixth title in thriller |
https://www.bbc.co.uk/sport/rugby-union/65727086?at_medium=RSS&at_campaign=KARANGA
|
clinch |
2023-05-27 16:05:18 |
ニュース |
BBC News - Home |
Bayern Munich beat Cologne 2-1 and Borussia Dortmund draw means Bayern are champions |
https://www.bbc.co.uk/sport/football/65714772?at_medium=RSS&at_campaign=KARANGA
|
Bayern Munich beat Cologne and Borussia Dortmund draw means Bayern are championsBayern Munich win an th successive Bundesliga title as they overhaul Borussia Dortmund on the final day of the season |
2023-05-27 16:55:21 |
IT |
週刊アスキー |
SFX-Lサイズで12VHPWRコネクター装備のコンパクト電源 |
https://weekly.ascii.jp/elem/000/004/138/4138499/
|
corsair |
2023-05-28 01:03:00 |
コメント
コメントを投稿