簡體   English   中英

如何在列表中比較python中的字符串?

[英]How to compare strings in python in List?

我最近開始使用Python工作。 我需要在列表中比較Python中的字符串,但不確定如何解決-

下面是我的代碼-

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)有一個或兩個或三個孩子,則將打印出類似的內容-

[u'test1']

or

[u'test1', u'test2']

or

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

我需要檢查是否children包含workflow串與否。 現在,它僅包含test1, test2, test3但將來它可以具有工作流以及test1, test2, test3, workflow

如果包含workflow 然后,我將只打印workflow ,而不打印其他任何內容。

注意:-get_children返回列表,如我猜的文檔所示

知道如何做到這一點嗎?

更新: -

如果將工作流節點相加,那么當我打印出子節點時,它將顯示如下:

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

因此,我需要檢查子級是否包含工作流,如果不包含子工作流,那么我們將不執行任何操作,但如果包含子工作流,則在提取子工作流后,僅打印出test1,test2,test3而不打印出來。

'workflow' in children

如果不是孩子則返回True/False

然后,您的代碼將是:

if 'workflow' in children: print 'workflow'

您可以使用內聯if語句。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM