None用法+连接字符串优先使用join +用format而不是%+区别可变对象和不可变对象(list的深拷贝和浅拷贝)
2. None这个东东
# 判断list是否为空
list = []if list is None: # 错误的方式print("list is None")
else:print("list is not None")if list: # 正确的方式print("list is not None")
else:print("list is None")"""
output:
list is not None
list is None
"""
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3. 连接字符串优先使用join而不是+
原因:
str1, str2, str3, str4, str5 = 'my', 'heart', 'will', 'go', 'on'
combine_str = ''.join([str1, str2, str3, str4, str5])
print(combine_str)
"""
output:
myheartwillgoon
"""
- 1
- 2
- 3
- 4
- 5
- 6
- 7
4. 用format而不是%
weather = ['a', 'b', 'c']
formatter = 'letter is: {0}'.format
for item in map(formatter, weather):print(item)
"""
output:
letter is: a
letter is: b
letter is: c
"""
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
注意:
list深拷贝和浅拷贝
list1=['a','b','c']
list2=list1
list1.append('d')
list2
['a', 'b', 'c', 'd']
import copy
list3=copy.deepcopy(list1)
list3
['a', 'b', 'c', 'd']
list3.append('e')
list3
['a', 'b', 'c', 'd', 'e']
list2
['a', 'b', 'c', 'd']
list1
['a', 'b', 'c', 'd']
list1.append('f')
list1
['a', 'b', 'c', 'd', 'f']
list2
['a', 'b', 'c', 'd', 'f']
list3
['a', 'b', 'c', 'd', 'e']
deepcopy的作用相当于是把list3与list2、list1完全隔开了。
append表示,list1和list2是完全等同的。
None用法+连接字符串优先使用join +用format而不是%+区别可变对象和不可变对象(list的深拷贝和浅拷贝)相关推荐
- python字符串转整数_Python连接字符串和整数
python字符串转整数 Python supports string concatenation using + operator. In most of the programming langu ...
- Python连接字符串用join还是+
我们先来看一下用join和+连接字符串的例子 str1 = " ".join(["hello", "world"]) str2 = &quo ...
- Join Strings with join(连接字符串)freeCodeCamp上边的项目
Join Strings with join(连接字符串) 我们可以使用join方法来把数组转换成字符串,里边的每一个元素可以用你指定的连接符连接起来,这个连接符就是你要传入的参数. 下面展示了使用j ...
- .Net配置文件中数据库中连接字符串用法总结
一.如果是Access数据库有以下几种种写法: 如果是连接Access数据库 <?xml version="1.0" encoding="utf8" ...
- 详解Python中的join()函数的用法(字符串和os.path)
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔 ...
- Python join() 函数 连接字符串
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的 ...
- MySQL的行转列、列转行、连接字符串 concat、concat_ws、group_concat函数用法
转载自:https://blog.csdn.net/aya19880214/article/details/41280893 1.concat函数 使用方法: CONCAT(str1,str2,-) ...
- 在Python中连接字符串的首选方法是什么?
本文翻译自:Which is the preferred way to concatenate a string in Python? Since Python's string can't be c ...
- 如何有效地连接字符串
在Go中, string是基本类型,这意味着它是只读的,并且对它的每次操作都将创建一个新字符串. 因此,如果我想在不知道结果字符串长度的情况下多次连接字符串,那么最好的方法是什么? 天真的方式是: s ...
最新文章
- 澳洲专升硕计算机专业,澳洲计算机专升硕-纽卡斯尔大学
- Windows PE第6章 栈与重定位表
- 用户态与内核态的区别
- 前端代码(移动端app, vue 源代码)打包方法
- centos xampp安装mysql_在xampp上部署dvwa|centos(linux)环境
- 怎样教一台计算机区分猫和狗?一文零基础入坑机器学习
- Python3+RobotFramework+pycharm
- 借给朋友两万块钱,已经两年,每次要钱都各种借口,我该怎么办?
- 图解:SQL SERVER2005的安装
- 如何修改硬盘挂载的名字LABEL
- 网站如何优化才是成功的
- 解决C#中ListView闪烁的办法
- Java-重复性代码统计第一篇
- 科学计算机开多次方根号,计算器开多次方怎么操?
- wind10 使用Hyper-V安装centos7遇到的问题及解决方案
- 齐桓公称霸天下的用人之道
- wangeditor富文本编辑器集成配置
- ubantu18.10安装mysql
- c语言中5l是数值或字符常量,C语言练习题-选择题(带答案)
- el-table合并一列中数据相同的单元格
热门文章
- WPF in Visual Studio 2010
- 解决后退,清空验证码(其它文本框保留)
- VB 6.0中的数据连接模块
- phpstudy+dvwa搭建
- 简单理解Tomasulo算法与重加载缓冲区
- greenplum配置高可用_Greenplum 的高可用是怎么做到的?
- 权限不同,设置标签绑定的事件生效与不生效(使事件不起作用)
- windows安装go环境变量
- [JavaScript编程练习]js获取文字中的100,使其变为红色,若文字100改为其他任意数字,该数字依然会是红色
- Warning:关于_CRT_SECURE_NO_WARNINGS