博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python-小知识点积累(持续更新)
阅读量:3938 次
发布时间:2019-05-23

本文共 1974 字,大约阅读时间需要 6 分钟。

Operators

  • 取整 double slash //

    = math.floor()

  • 取余 percentage / modulus operator %

  • 逻辑运算符:

    · And: &
    · Or: |
    · XOR: ^
    计算过程:将数字换算成二进制后,每一位分别进行逻辑XOR的运算
    6 ^ 2
    2 = 1 0
    6 = 1 1 0
    ————
    == 1 0 0 =》 4

  • ord() 中间放字符,可以显示该字母的unicode码,如ord(a) = 97

  • chr(97) = ‘a’, 是ord()的反向

Functions

  • eval()
    将string转化为float或int。
eval("4.05")4.05  # floateval("3 + 4")7  # int
  • capitalize()
    将整句的第一个单词首字母变为大写。

Shallow Copy & Deep Copy

python中copy()函数与list[ : ]使用的就是shallow copy,当original list只有一层结构没有嵌套时,不会有什么问题,对新list的更改并不会改变老list。

但当original list为一个嵌套的结构,里面有其他list作为element时,就会出现问题了。

import copy## shallow copyoriginal_list = [1, 2, [3,5], 4] new_list = copy.copy(original_list)  # 将new_list中的3 改变为 7 后:The original elements before shallow copying1 2 [3, 5] 4 The original elements after shallow copying1 2 [7, 5] 4 ## deep copyoriginal_list = [1, 2, [3,5], 4] new_list = copy.deepcopy(original_list) # 将new_list中的3 改变为 7 后:The new list of elements after deep copying 1 2 [7, 5] 4 The original elements after deep copying1 2 [3, 5] 4

以下内容是更详细的解释,援引自下面的网址

https://www.geeksforgeeks.org/copy-python-deep-copy-shallow-copy/

  • Deep copy is a process in which the copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In case of deep copy, a copy of object is copied in other object. It means that any changes made to a copy of object do not reflect in the original object. In python, this is implemented using “deepcopy()” function.

在这里插入图片描述

  • A shallow copy means constructing a new collection object and then populating it with references to the child objects found in the original. The copying process does not recurse and therefore won’t create copies of the child objects themselves. In case of shallow copy, a reference of object is copied in other object. It means that any changes made to a copy of object do reflect in the original object. In python, this is implemented using “copy()” function.

在这里插入图片描述

转载地址:http://zquwi.baihongyu.com/

你可能感兴趣的文章
How package finding works
查看>>
build opencv3.3.0 with VTK8.0, CUDA9.0 on ubuntu9.0
查看>>
how to compile kinfu_remake with cuda 9.0 opencv2.4.13.4
查看>>
qtcreator4.4.1中cmake 与cmake3.5.1本身generate出来的setting是有区别的解决方法
查看>>
ubuntu下解决csdn网页打不开的问题
查看>>
MySQL server has gone away 问题的解决方法
查看>>
MySQL十大优化技巧
查看>>
PHP中文件读写操作
查看>>
php开发常识b_01
查看>>
PHP单例模式
查看>>
PHP项目设计
查看>>
memcache的安装及管理
查看>>
git 传输
查看>>
创建新项目
查看>>
印刷工艺- 喷墨印刷
查看>>
印刷工艺流程
查看>>
印刷业ERP启蒙
查看>>
Java8 Lambda表达式使用集合(笔记)
查看>>
Java魔法师Unsafe
查看>>
spring cloud java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
查看>>