更新的答案
在问题的上下文中,我们正在处理伪代码,但是从Python
3.8开始,
:=它实际上是一个有效的运算符,它允许在表达式中分配变量:
# Handle a matched regexif (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 outputfiltered_data = [y for x in data if (y := f(x)) is not None]
有关更多详细信息,请参见PEP 572。
原始答案
您发现的是 伪代码
伪代码 是计算机程序或其他算法的工作原理的非正式高级描述。
:=实际上是赋值运算符。在Python中,这很简单
=。
要将伪代码转换为Python,您需要了解所引用的数据结构,以及更多的算法实现。
有关伪代码的一些注意事项:
:=
是赋值运算符或=
在Python中=
是等于运算符或==
在Python中- 有某些款式,您的里程可能会有所不同:
帕斯卡风格
procedure fizzbuzzFor 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
C风格
void function fizzbuzzFor (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;}注意花括号用法和赋值运算符的区别。



