python - Assigning values conditionally (with multiple conditions) -


as follow-on original question: python: stripping elements of string array based on first character of each element

i'm wondering if can expand if statement:

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

to include , 2nd condition:

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

this generates syntax error hope there's alternative syntax can use!

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

you need change

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

to

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

or parenthesis removed: (as per @viraptor's suggestion)

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

note if(...), ... must contain logic not if(word.startswith("/")). , replace & bitwise operator and.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -