2

I have recently started working wth Python. I need to compare strings in Python in the List and I am not sure how this will work out -

Below is my code -

def my_func ( event ):
  print event
  print (event.type)
  if event.type == EventType.CHILD:
    children=zk.get_children("/ss/testing", watch=my_func)
    print(children)

print(children1) will print out something like this, if it has one children or two or three-

[u'test1']

or

[u'test1', u'test2']

or

[u'test1', u'test2', u'test3']

I need to check whether children contains workflow string or not. Right now it only contains test1, test2, test3 but in future it can have workflow as well like test1, test2, test3, workflow

If it contains workflow. Then I will print out only workflow and nothing else.

NOTE:- get_children returns List as shown in the documentation I guess

Any idea how this can be done?

UPDATE:-

If workflow nodes gets added up then it will show like this if I print out children-

[u'test1', u'test2', u'test3', u'workflow']

So I need to check whether children contains workflow or not, if it doesn't contains, then we won't do anything but if it contains, then we will print out workflow only not test1, test2, test3 after extracting from it.

4
  • Is workflow a string? Commented Nov 20, 2013 at 22:00
  • "workflow" in children should work ,., did you even try googling this first? Commented Nov 20, 2013 at 22:01
  • @DylanLawrence: I updated my question with more details. Commented Nov 20, 2013 at 22:03
  • @JoranBeasley: I am not sure I understand what you said about "workflow" in children should work? btw, I have updated my question with some more details. Commented Nov 20, 2013 at 22:04

2 Answers 2

4
'workflow' in children

returns True/False if it is/isn't in children

Then, your code would be:

if 'workflow' in children: print 'workflow'
Sign up to request clarification or add additional context in comments.

Comments

2

You can use an in line if statement.

print('workflow' if 'workflow' in children else children)

3 Comments

That's not an inline if-else, it's called the ternary conditional operator.
Not to get bogged down in a symantic debate, but it is referred to as an in line if statement commonly (google it), and it is also an accurate description of it.
python.org/dev/peps/pep-0308 I find the word ternary , conditional and operator but no inline. I guess inline if-else must be just colloquial for what I was saying.

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.