![](/img/trans.png)
[英]why can't I use walrus in a one-line for expression
我正在嘗試在 for 循環中使用海象來創建一個列表, 像這樣的東西: data = [l:= line.strip().somefunc() for line in iterable_obj if(l[0] == 'sth')] 但它返回一個空列表有人可以提示我在這里做錯了什么嗎? ...
[英]why can't I use walrus in a one-line for expression
我正在嘗試在 for 循環中使用海象來創建一個列表, 像這樣的東西: data = [l:= line.strip().somefunc() for line in iterable_obj if(l[0] == 'sth')] 但它返回一個空列表有人可以提示我在這里做錯了什么嗎? ...
[英]Walrus operator in dict declaration
我想在字典聲明中使用海象運算符。 但是:可能會導致問題。 我有一個嵌套在列表理解中的字典聲明,但我不想將它分解為一個簡單的 for 循環(這將是一個懶惰的答案)。 甚至可能嗎? 這在一些簡單的場景中也很有用。 注意: dict理解中的海象運算符沒有回答我的問題,因為我沒有可以使用海象運算符的條件。 ...
[英]Underscore variable with walrus operator in Python
在 Python 中,變量名_ (下划線)通常用於一次性變量(永遠不會使用的變量,因此不需要正確的名稱)。 使用海象運算符:= ,我看到需要一個相當短暫的變量(例如僅用於一行代碼)。 我想知道在這種情況下使用_是否也是合理的,或者它是否可能會讓閱讀代碼的人感到困惑? 例子: a = (dummy ...
[英]Warlus Operator conversion
我有一段涉及海象運算符的代碼。 我正在嘗試將其轉換為普通的 python 代碼。 但我不確定它是否正確發生。 # code with warlus NUM_ELEMS = cpu_count() NUM_CORES = len(list_of_data) fair_core_worload = N ...
[英]Can't get the walrus operator to work (Python double list comprehension)
此列表理解不起作用: NameError: name 'buylow' is not defined 這個也沒有: SyntaxError: assignment expression cannot be used in a comprehension iterable expression 我該 ...
[英]python: walrus operator and re.search() in list comprehension
我有一個字符串列表,我想從元素中提取模式。 例如,給定列表["A 12345bcd", "BYT 676 CCC"]和模式r'\d\d\d\d\d' ,我想獲得: ["12345", ""] 我知道沒有它怎么辦,但我想使用海象運算符:= 。 我試過了: [(m:=re.search(r'\d\d\ ...
[英]Two Walrus Operators in one If Statement
有沒有正確的方法在 1 個 if 語句中有兩個海象運算符? 此示例適用於three ,但five將“未定義”。 ...
[英]Can this loop be done in a list comprehension?
來自這個問題將數組的最后一個 position 添加到同一個數組我很好奇上述循環是否可以在列表理解中完成?array = [3,4,2,5,4,5,8,7,8,9] value = 10 for i in range(1,10): array[i] = array[i-1] + value ...
[英]How to properly use assignment operator with mod operator in an if statement?
我正在嘗試在 python 中使用具有不同 if 語句的海象運算符,並且我嘗試替換的代碼如下所示: 預期的 output 應如下所示: 現在,如果嘗試用海象運算符(概念)替換我的代碼,如果我嘗試以下操作,它確實會給我結果中的 True 或 0: Output: 或者: output: 現在我知 ...
[英]Walrus Operator Doesn't Assign Variable?
與海象運算符一起玩,我有這個合並排序的實現: def mergesort(array): if len(array) == 1: output = array else: pivot = len(array) // 2 left = m ...
[英]yield with walrus operator := causes syntax error
旨在創建一個新的 Excel 應用程序並每次調用其退出方法。 我認為它可以用 walrus 運算符更簡潔地編寫,但我得到SyntaxError: invalid syntax ...
[英]Why isn't the walrus operation a valid statement?
我在終端上做一些 python,在某個時候我寫了x := 1並且它顯示了一個語法錯誤。 我知道 walrus 運算符是一個表達式,但其他表達式可以完美運行。 甚至海象運算也在括號內起作用。 所以我的問題是,為什么其他所有表達式都可以作為語句有效,而 walrus 卻不是? ...
[英]walrus operator in dict comprehension
我想避免在字典理解中對均值進行雙重評估,我嘗試使用海象運算符: 但這給了我以下錯誤: 此錯誤僅在我嘗試使用變量時發生,定義它時沒有問題: 為什么? 我哪里弄錯了? ...
[英]Python 3.8 Walrus Operator with not and assigning mutliple variables
我正在研究 selenium 包裝器。 我想檢查網頁上的元素是否可見。 function 獲取遵循模式"selector=value"的輸入變量selector ,例如"id=content" ,但也可能是這個"link_text=Hello World" 。 搜索 function 將該字符串分 ...
[英]Walrus operator in Python interpreter
當我在 Python(3.9.6) 解釋器中使用如下海象運算符時, 我收到語法錯誤: 這與以下有何不同? ...
[英]Build dict from list of pairs that is made from string
假設我們有一個像這樣的字符串s = "a=b&c=d&xyz=abc" 我想要字典{"a": "b", "c": "d", "xyz": "abc"} 這是實現此目標的一種方法:dict([item.split("=") for item in s.split("&")] ...
[英]Can you combine the addition assignment ( += ) operator with the walrus operator ( := ) in Python?
這是我現在寫的代碼: a = 1 if (a := a + 1) == 2: print(a) 我想知道是否存在這樣的事情: a = 1 if (a +:= 1) == 2: print(a) ...
[英]How to type hint with walrus operator?
我正在嘗試輸入提示海象運算符表達式,即while (var: int:= some_func()): ... 我怎樣才能做到這一點? ...
[英]Using a walrus operator in if statement does not work
我有一個簡單的 function 應該 output 一個基於模式的前綴或None如果它不匹配。 嘗試做海象似乎不起作用。 任何想法? 追溯 ...
[英]Why does using the walrus operator on a member variable raise a SyntaxError?
為什么我不能使用海象運算符:=來分配屬性? 它在分配給局部變量時起作用: 但是,如果我嘗試分配給 object 屬性,則會出現語法錯誤: 我的意思是我可以使用臨時局部變量繞過錯誤,但為什么會發生這種情況? 我相信 100% 這是一種合法的語法。 ...