1

この元の質問の続きとして: Python: Stripping elements of a string array based on first character of each element

このifステートメントを展開できるかどうか疑問に思っています:

with open(bom_filename, 'r') as my_file:
    file_array = [word.strip() for word in my_file if word.startswith("/")]

含めて2番目の条件:

with open(bom_filename, 'r') as my_file:
    file_array = [word.strip() for word in my_file if (word.startswith("/")) & not(word.endswith("/"))]

これにより構文エラーが発生しますが、使用できる代替構文がいくつかあることを願っています!

4

1 に答える 1

1
with open(bom_filename, 'r') as my_file:
    file_array = [word.strip() for word in my_file if (word.startswith("/") and not(word.strip().endswith("/")))]

変更する必要があります

if (word.startswith("/")) & not(word.endswith("/"))

if (word.startswith("/") and not(word.strip().endswith("/"))) 

または余分な括弧を削除して:(@viraptorの提案による)

if word.startswith("/") and not word.strip().endswith("/") 

だけでなく、すべてのロジックを含める必要があることif(...)に注意してください。そして、ビット単位の演算子である which を に置き換えます。...if(word.startswith("/"))&and

于 2013-05-14T13:39:00.707 に答える