通过id( )我们可以查看到一个变量表示的值在内存中的地址
a = '1234abcd' b = '1234abcd' c = '12345678' print(id(a)) # 64823984 print(id(b)) # 64823984 print(id(c)) # 64889456通过id( )计算出来的结果,比较数据对象的内存地址,我们可以查看两个变量使用的是否为同一对象
# == 双等表示的是判断数据的值是否相等 # is判断数据的内存地址是否相等 s1 = "哈哈" s2 = "哈哈" print(s1 == s2) # True print(s1 is s2) # True 因为有小数据池所以变量地址相同 l1 = [1, 2, 3] l2 = [1, 2, 3] print(l1 == l2) # True print(l1 is l2) # False小数据池:
对于整数,Python官方文档中这么说: The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. </font> 对于字符串: Incomputer science, string interning is a method of storing only onecopy of each distinct string value, which must be immutable. Interning strings makes some stringprocessing tasks more time- or space-efficient at the cost of requiring moretime when the string is created or interned. The distinct values are stored ina string intern pool. –引自维基百科 </font>表达的意思就是: Python自动将-5~256的整数进行了缓存,当你将这些整数赋值给变量时,并不会重新创建对象,而是使用已经创建好的缓存对象。 python会将一定规则的字符串在字符串驻留池中,创建一份,当你将这些字符串赋值给变量时,并不会重新创建对象, 而是使用在字符串驻留池中创建好的对象。
建议参考大佬们的文档: https://www.cnblogs.com/jin-xin/articles/9439483.html https://blog.csdn.net/z_bright/article/details/84637852
唯唯诺诺的个人理解:我们创建代码的时候系统会自动创建内存地址用来代码运行,小数据池是把整数、字符串、bool这种占用空间小的数据固定存储起来,我们要用的时候,直接去系统提前存好的的空间取,不用再次创建新的空间,从而节省内存空间(对比大佬们详细的解答,感觉自己弱爆了 QAQ捂脸)
官文:The string whose method is called is inserted in between each given string. The result is returned as a new string. 翻译:调用其方法的字符串插入到每个给定字符串之间。 结果将作为新字符串返回。
a = 'abc' a1 = a.join('123') # 把字符串a插入到'123'中 print(a1) # 1abc2abc3 s = '_'.join(['abc','def','ghe']) print(s) # abc_def_ghe print(type(s)) # <class 'str'> # 结果总结:' '.join()能把列表变成字符串,而把字符串变成列表用str.split()因为:拷贝比创建对象的过程要快
bytes的表现形式: 1. 英文 b’ali’ 英文的表现形式和字符串没什么两样 2. 中文 b’\xe4\xb8\xad’ 这是一个汉字的UTF-8的bytes表现形式
s = "ali" print(s.encode("utf-8")) # b'ali' 将字符串编码成UTF-8 print(s.encode("GBK")) # b'ali' 将字符串编码成GBK s = "中" print(s.encode("UTF-8")) # b'\xe4\xb8\xad' 中文编码成UTF-8 print(s.encode("GBK")) # b'\xd6\xd0' 中文编码成GBK用什么方式编码,用什么方式解码
s = "我叫xxx" print(s.encode("utf-8")) # 编码:b'\xe6\x88\x91\xe5\x8f\xabxxx' print(b'\xe6\x88\x91\xe5\x8f\xabxxx'.decode('utf-8')) # 解码:我叫xxx print(b'\xe6\x88\x91\xe5\x8f\xabxxx'.decode('GBK')) # 解码:鎴戝彨xxx ???否则就乱码看不懂了 s = "我是文字" wz = s.encode("GBK") # 我们这样可以获取到GBK的文字 # 把GBK转换成UTF-8 # 首先要把GBK转换成unicode. 也就是需要解码 s = wz.decode("GBK") # 解码 # 然后需要进行重新编码成UTF-8 wzz = s.encode("UTF-8") # 重新编码 print(wzz) # b'\xe6\x88\x91\xe6\x98\xaf\xe6\x96\x87\xe5\xad\x97'