-
Notifications
You must be signed in to change notification settings - Fork 0
347. Top K Frequent Elements.md #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
count_frequency[num] += 1 | ||
else: | ||
count_frequency[num] = 1 | ||
return list(map(lambda x:x[0], sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一行に詰め込み過ぎており、読むにあたり認知負荷が高く感じられました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fhiyo/leetcode#17 (comment)
7つあたりが限界に思いますね。
それに、これ、実行順序が、items, sorted, slice, map, list ですよね。目が左右に動きます。
sorted の結果を一回変数に置きたいです。
それはそうとして、文法への慣れの問題はあると思っています。いくつか案。
list(map(lambda x:x[0], items))
この形は
[k for k, _ in items]
のほうが見やすいでしょう。
また、sorted を使うならば、
sorted(count_frequency, key=count_frequency.get, reverse=True)[:k]
も手です。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
文法への慣れの問題
すごくご指摘通りで、リストの内包表記が慣れないなと思ってるところでした。(書いてあったら読めるんですが、どうも自分で書く際の選択肢に出てこないです。。)
慣れ、ということでしばらく挑戦してみます!
if num in count_frequency: | ||
count_frequency[num] += 1 | ||
else: | ||
count_frequency[num] = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これも defaultdict 使わないとしてもいくつかあって、
if num not in count_frequency:
count_frequency[num] = 0
count_frequency[num] += 1
か
count_frequency.setdefault(num, 0)
count_frequency[num] += 1
か
count_frequency[num] = count_frequency.get(num, 0) + 1
ですかね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
以下自分メモ:
dictが持ってるメソッドを確認した。
https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、そうそう。これを開いて読むの大事です。翻訳でもいいです。
if num in count_frequency: | ||
count_frequency[num] += 1 | ||
else: | ||
count_frequency[num] = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こっちの書き方のほうが個人的には好きです。(が、趣味の範疇な気がします)
if num in count_frequency: | |
count_frequency[num] += 1 | |
else: | |
count_frequency[num] = 1 | |
if num not in count_frequency: | |
count_frequency[num] = 0 | |
count_frequency[num] += 1 |
for key, value in sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k]: | ||
top_k_elements.append(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ループの対象がわかりづらいので、事前に別変数で定義したほうが可読性が上がりそうです。(frequency_ranking
は ChatGPT が変数名案として返してきたものなんですが、意外とわかりやすいかもと思ってここで採用してみました)
また key
, value
は汎用的な用語なので、この問題に適した変数名にしたほうがよさそうです。(value
は使っていないのでそもそも _
でよいですね)
for key, value in sorted(count_frequency.items(), key=lambda x:x[1], reverse=True)[:k]: | |
top_k_elements.append(key) | |
frequency_ranking = sorted(count_frequency.items(), key=lambda x:x[1], reverse=True) | |
for element, _ in frequency_ranking[:k]: | |
top_k_elements.append(element) |
- 別のパターン | ||
- `dict()`のかわりに`defaultdict()`をつかう | ||
- https://github.com/thonda28/leetcode/pull/17/files#r1769492893 | ||
- top kを選ぶときにheapqを使う。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
優先度付きキューとかは割と見るイメージがあるので、(heapq ライブラリではなく)自分で一度実装してみるのもおもしろいと思います。
if num in count_frequency: | ||
count_frequency[num] += 1 | ||
else: | ||
count_frequency[num] = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
step2以降でお気づきですが、default_dictでこのあたりの初期化処理をなくせそうですね
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
実はPythonだとCounterにmost_commonというのがあり、簡単にかけます。とはいえこの問題が求めていることではないでしょうが。
https://docs.python.org/3/library/collections.html#collections.Counter
https://docs.python.org/3/library/collections.html#collections.Counter.most_common
https://github.com/python/cpython/blob/d610d821fd210dce63a1132c274ffdf8acc510bc/Lib/collections/__init__.py#L619
まああとはクイックセレクトとかバケットソートとかでも書いてみると良いと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
実はPythonだとCounterにmost_commonというのがあり、簡単にかけます。とはいえこの問題が求めていることではないでしょうが。
https://docs.python.org/3/library/collections.html#collections.Counter
https://docs.python.org/3/library/collections.html#collections.Counter.most_common
https://github.com/python/cpython/blob/d610d821fd210dce63a1132c274ffdf8acc510bc/Lib/collections/__init__.py#L619
まああとはクイックセレクトとかバケットソートとかでも書いてみると良いと思います。
https://leetcode.com/problems/top-k-frequent-elements/description/