栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python中冒号等于(:=)是什么意思?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python中冒号等于(:=)是什么意思?

更新的答案

在问题的上下文中,我们正在处理伪代码,但是从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;}

注意花括号用法和赋值运算符的区别。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516782.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号