6

キーの順序を保持する連想配列が必要な場合は、collections.OrderedDictを使用することがあります。これの最良の例は、csv ファイルの解析または作成です。この場合、列の順序を暗黙的にオブジェクトに保持すると便利です。

しかし、連想配列の全体的な概念は、キーの順序は決して重要ではなく、順序付けに依存する操作はすべてリストを使用する必要があるように思われるため、これは悪い習慣ではないかと心配しています。リストが存在します (これは上記の csv の例で実行できます)。これに関するデータはありませんが、リストのパフォーマンスが OrderedDict よりも全体的に優れていることは間違いありません。

だから私の質問は次のとおりです: OrderedDict の本当に説得力のある使用例はありますか? csv の使用例は、使用すべき場所の良い例ですか、それとも悪い例ですか?

4

5 に答える 5

6

But I'm worried that this is bad practice, since it seems to me that the whole concept of an associative array is that the order of the keys should never matter,

Nonsense. That's not the "whole concept of an associative array". It's just that the order rarely matters and so we default to surrendering the order to get a conceptually simpler (and more efficient) data structure.

and that any operations which rely on ordering should just use lists because that's why lists exist

Stop it right there! Think a second. How would you use lists? As a list of (key, value) pairs, with unique keys, right? Well congratulations, my friend, you just re-invented OrderedDict, just with an awful API and really slow. Any conceptual objections to an ordered mapping would apply to this ad hoc data structure as well. Luckily, those objections are nonsense. Ordered mappings are perfectly fine, they're just different from unordered mappings. Giving it an aptly-named dedicated implementation with a good API and good performance improves people's code.

Aside from that: Lists are only one kind of ordered data structure. And while they are somewhat universal in that you can virtually all data structures out of some combination of lists (if you bend over backwards), that doesn't mean you should always use lists.

I don't have data on this, but I'm willing to bet that the performance for lists is universally better than OrderedDict.

Data (structures) doesn't (don't) have performance. Operations on data (structures) have. And thus it depends on what operations you're interested in. If you just need a list of pairs, a list is obviously correct, and iterating over it or indexing it is quite efficient. However, if you want a mapping that's also ordered, or even a tiny subset of mapping functionality (such as handling duplicate keys), then a list alone is pretty awful, as I already explained above.

于 2013-06-30T18:59:13.270 に答える
2

特定のユース ケース (csv ファイルの書き込み) では、順序付けされた dict は必要ありません。代わりに、を使用してDictWriterください。

個人的にはOrderedDict、LIFO/FIFO アクセスが必要なときに使用します。そのためのpopitem方法もあります。正直なところ、良いユースケースは思いつきませんでしたが、属性の順序についてPEP-0327で言及されているものは良いものです:

XML/HTML 処理ライブラリは現在、属性の順序付けをやめたり、辞書の代わりにリストを使用してフィルタリングを面倒にしたり、独自の順序付き辞書を実装したりしています。これは、ElementTree、html5lib、Genshi、およびその他の多くのライブラリに影響します。

なぜ Python に何らかの機能があるのか​​疑問に思っている場合は、PEP から始めるのが良いでしょう。なぜなら、その機能を含めることにつながる正当な理由が詳しく説明されているからです。

于 2013-06-30T18:42:00.433 に答える
0

多分コメントで十分です...

必要のない場所(順序が関係なく、通常の辞書で十分な場合)で使用すると疑わしいと思います。それ以外の場合、コードはおそらくリストを使用するよりも単純になります。

これは、どの言語構造/ライブラリにも有効です。コードを単純にする場合は、より高いレベルの抽象化/実装を使用してください。

于 2013-06-30T18:42:43.653 に答える
0

このデータ構造に満足し、ニーズに合っている限り、気にする必要はありません。おそらく、(速度などの点で) より効率的なものではないかもしれませんが、ある場合は、特定の場合に役立つためです (または、誰もそれを書くことを考えなかったでしょう)。

Python では、基本的に 3 種類の連想配列を使用できます。

  1. 従来のハッシュ テーブル (順序なし)
  2. OrderedDict(オブジェクトが作成された方法を反映する順序)
  3. およびバイナリツリー-これは標準のlibにはありません-キーをカスタムオーダーで正確に並べます(必ずしもアルファベット順ではありません)。

したがって、実際には、キーの順序重要になる場合があります。仕事をするのにより適していると思う構造を選択してください。

于 2013-06-30T18:58:09.610 に答える