Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Pythonでは、次のことを行うための最も好ましい(pythonic)方法は何ですか:
あなたはリストを与えられます。リストが空でない場合、リスト内のすべての項目が文字列であることが保証されます。リスト内の各項目は、空の文字列であるか、項目に対して が呼び出されたTrue場合に返されることが保証されています。isdigit()
True
isdigit()
そのようなリストから始めて、空の文字列を除いて、元のリストのすべての項目を含むようなリストになる最もエレガントな方法は何ですか?
filter()デフォルトの同一性関数 ( ) での使用None:
filter()
None
newlist = filter(None, origlist)
または、リスト内包表記:
newlist = [el for el in origlist if el]