- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
:=
操作数是什么意思,更具体地说,对于 Python?
谁能解释一下如何阅读这段代码?
node := root, cost = 0
frontier := priority queue containing node only
explored := empty set
最佳答案
在问题的上下文中,我们正在处理伪代码,但是 starting in Python 3.8 , :=
实际上是一个有效的运算符,允许在表达式中分配变量:
# Handle a matched regex
if (match := pattern.search(data)) is not None:
# Do something with match
# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
process(chunk)
# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]
# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]
见 PEP 572了解更多详情。
你找到的是pseudocode
Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm.
:=
实际上是赋值运算符。在 Python 中,这只是 =
。
要将此伪代码翻译成 Python,您需要了解所引用的数据结构,以及更多的算法实现。
关于伪代码的一些注意事项:
:=
是赋值运算符或 Python 中的 =
=
是相等运算符或 Python 中的 ==
procedure fizzbuzz
For i := 1 to 100 do
set print_number to true;
If i is divisible by 3 then
print "Fizz";
set print_number to false;
If i is divisible by 5 then
print "Buzz";
set print_number to false;
If print_number, print i;
print a newline;
end
void function fizzbuzz
For (i = 1; i <= 100; i++) {
set print_number to true;
If i is divisible by 3
print "Fizz";
set print_number to false;
If i is divisible by 5
print "Buzz";
set print_number to false;
If print_number, print i;
print a newline;
}
注意大括号用法和赋值运算符的区别。
关于python - 冒号等于什么(:=) in Python mean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26000198/
我正在尝试使用字符串类型的变量动态调用函数,即变量将包含字符串形式的函数名称。因此,我需要使用该变量调用函数。 所以,我正在使用 Scala Reflection。如果函数接受 Sting 数据类型,
class X extends Y { X(int a, int b) : super(a,b); } 有人可以给我解释冒号:的语法含义吗? 最佳答案 Dart 中的此功能称为“初始化列表”。 它
我正在寻找一些为旧的 jQuery 版本 v.1.6.4 编写的 jquery 代码。它有一个多属性选择器,如下所示: "span:[id^='someid-']" 选择器用于各种功能,如“查找”
This question already has answers here: How do you access the matched groups in a JavaScript regular
显然,冒号在 Java 中有多种使用方式。有人介意解释一下它的作用吗? 例如这里: String cardString = ""; for (PlayingCard c : this.list) /
我在用 c 编程,我将 c 代码编译为汇编代码,但是当我用 NASM 汇编程序重新编译代码时,它给我一个错误 操作数后预期的逗号、冒号、修饰符或行尾。这发生在 第 6 行,第 7 行和第 8 行。请帮
我正在寻找一个字符来使用文件名定界符(我将多个文件名存储在一个纯文本字符串中)。 Windows 似乎不允许 : , ? , * , , " , | , /和 \在文件名中。显然,\和 /不能使用,
我有一个变量 $entry 设置为: stdClass Object ( [im:name] => stdClass Object ( [label] => Amazing Breaker ) etc
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the h
本文实例讲述了Python数据类型中的“冒号“[::]——分片与步长操作。分享给大家供大家参考,具体如下: 例如有如下字符串: ?
我创建了一个将 Excel 文件转换为 Access 数据库的应用程序。在转换过程中,MACAddress 列数据中的“:”符号需要替换为空格。 我尝试使用 replace 方法修改查询,但它不起作用
我在 ruby 中将散列转换为 yaml,但它添加了 : 到键:例如: :name: "Name" :value: "Value" 如果有办法避免并输出: name: "Name" value:
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: VB Using colons to put two statements on same line 我在 Exce
我的 createSQLQuery() 方法使用具有 : 字符的表 Tables::IMS_EPISODE, 这将被视为a Paramater ,需要设置 有没有办法转义这个字符,这样它就不会被视为参
我收到了一个定义游戏“棋盘”对象的 Python 类,以及一些在构造函数中初始化的成员。大部分内容都很简单,但我不理解此代码段最后两行中集合初始化中“:”运算符的含义: class Board: BL
在我的元素中,有一个库生成元素的情况,我需要从那里选择特定的元素——恰好包含一个带有“:”的属性。 换句话说,我最终尝试选择使用:document.querySelectorAll("[xml:spa
这个问题在这里已经有了答案: Is there any difference between the `:key => "value"` and `key: "value"` hash notati
这个问题在这里已经有了答案: What does C++ struct syntax "a : b" mean (5 个回答) 关闭8年前。 struct _USBCHECK_FLAGS {
我在分配变量时不小心使用了 : 而不是 = ,令我惊讶的是它没有产生错误。例如,以下运行没有投诉: Python 3.7.4 (default, Jul 9 2019, 18:15:00) [Cla
我对定义/调用 Lua 函数时使用的冒号表示法感到非常困惑。 我以为我已经明白了,直到我看到这段代码: function string.PatternSafe( str ) return (
我是一名优秀的程序员,十分优秀!