簡體   English   中英

Python 語法:如何檢查返回值中是否存在“x”?

[英]Python Syntax: How do I check if 'x' exists in the return value?

我有一個 python 語法問題。

如果 result() 傳入 3 arguments 並因此返回 (x, y, z) 形式的內容,如何檢查返回值中是否存在“x”?

我想檢查 function result() 中的 thing、thing2 或 thing3 是否為“X”。 現在,我正在寫“雖然結果中的'X':”......我不認為我這樣做是正確的,因為結果不是字符串。

我只是想看看結果,可能是 (a,b,c) 或 (1, 2, 3) 或 (x, y, z) 是否包含“x”。 例如,如果結果是 (x,y,z) 並且我詢問結果是否包含“x”,它將返回 true。 如果我問同樣的問題,結果是 (a, b, c),它會返回 false。 我將如何 go 這樣做?

def result(thing, thing2, thing3):
  # Returns a new thing (deepcopied) with thing3 space taken by thing2
  new_thing = thing.copy()
  new_thing[thing3] = thing2
  return new_thing

def terminal(thing, k): 
  while 'X' in result: 

我認為while 'X' in result:語句不能按您期望的方式工作,因為result是 function 而不是 function 的 output result() 然而, terminal不能以明顯的方式調用結果,除非你想要類似result(thing, k, k)因為result有三個必需的 arguments 但terminal只有兩個輸入。

編輯:如果您知道result返回一個可迭代的(如列表或元組),那么while 'X' in result(arg1, arg2, arg3):沒有某種形式的手動索引result(arg1, arg2, arg3)[i]

暫無
暫無

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

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