私はこのループを持っている:2つのFORループとIF文を組み合わせるPythonicの方法はありますか?
for e in elements:
for h in hs:
if h.complete and e.date < h.date:
print('----completed at time----',)
は、1行またはPython的な方法でそれを書くための方法はありますか?
私はこのループを持っている:2つのFORループとIF文を組み合わせるPythonicの方法はありますか?
for e in elements:
for h in hs:
if h.complete and e.date < h.date:
print('----completed at time----',)
は、1行またはPython的な方法でそれを書くための方法はありますか?
1行
はいでそれを書くための方法はあります。
またはPythonicの方法で?
あなたが現在持っているものは、すでに最もPythonicな方法です。ここで何も変更する必要はありません。
これをいくつかの行に縮小するには、さまざまな方法がありますが、ほとんどが読みにくくなります。たとえば、次のように
ない-本当にリスト内包表記:[print('whatever') for e in elements for h in hs if e.date < h.date]
リスト内包:itertools.product
を使用してfor p in [sth(e, h) for e in elements for h in hs if e.date < h.date]: print(p)
:
for e, h in product(elements, hs):
if h.complete and e.date < h.date:
print('whatever')
filter
上記もののと同じ:
for e, h in filter(lambda e, h: h.complete and e.date < h.date, product(elements, hs)):
print('whatever')
編集:私の個人的な好みは、コードが実際に何を電報で良好である(唯一のオリジナルコードのオフ単一ラインをシェービングしながら)第product
例です。
私はあなたがする必要がありますが、私は表示されません。 –
2つの 'forループ 'に何も問題はありません。あなたはそれを1行に減らすことができますが、それは読みやすいものではありません。 – MooingRawr
'l = ['--- h.completeとe.dateの場合、要素のeのhsのh-のための----'; print(* l、sep = '\ n') ' –