9

How to merge the condition in array format

lines = [line for line in open('text.txt')if 
        '|E|' in line and
        'GetPastPaymentInfo' in line and
        'CheckData' not in line and
        'UpdatePrintStatus' not in line
        ]

like

lines = [line for line in open('text.txt')if 
        ['|E|','GetPastPaymentInfo'] in line and
        ['CheckData','UpdatePrintStatus'] not in line]
1
  • You can't; the list won't be in the line. You could use all. Commented Jun 12, 2015 at 8:01

1 Answer 1

8

You can use a generator expression within all function to check the membership for all elements :

lines = [line for line in open('text.txt') if 
        all(i in line for i in ['|E|','GetPastPaymentInfo'])and
        all(j not in line for j in ['CheckData','UpdatePrintStatus'])]

Or if you want to check for words you can split the lines and use intersection method in set 1:

lines = [line for line in open('text.txt') if 
        {'|E|','GetPastPaymentInfo'}.intersection(line.split()) and not {'CheckData','UpdatePrintStatus'}.intersection(line.split())]

Note that you need to put your words within a set instead of list.


1) Note that since set object use hash-table for storing its elements and for returning the items as well, checking the membership has O(1) order and it's more efficient than list which has O(N) order.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.