itertoolsチェーン()の結果をフィルタリングするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1129344

質問

私の見解では、itertoolsモジュールをインポートする場合:

from itertools import chain

そして、私はそれをいくつかのオブジェクトにチェーンします:

franktags = Frank.objects.order_by('date_added').reverse().filter(topic__exact='art') 
amytags = Amy.objects.order_by('date_added').reverse().filter(topic__exact='art') 
timtags = Tim.objects.order_by('date_added').reverse().filter(topic__exact='art') 
erictags = Eric.objects.order_by('date_added').reverse().filter(topic__exact='art')

ourtags = list(chain(franktags, amytags, timtags, erictags))

「Date_Added」で「OurTags」を注文するにはどうすればよいですか?

驚くことではありません、

ourtags = list(chain(franktags, amytags, timtags, erictags)).order_by('date_added')

「 'list'オブジェクトには属性「order_by」 "エラーがありません。

役に立ちましたか?

解決

import operator

ourtags = sorted(ourtags, key=operator.attrgetter('date_added'))

他のヒント

コードのこの時点までに、すべてのオブジェクトをメモリとリストに既にロードしています。古いPythonリストのようにリストを並べ替えてください。

>>> import operator
>>> ourtags.sort(key=operator.attrgetter('date_added'))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top