Python编程:小组技术分享会之is和==
【摘要】 # 字符串是不可变类型
s = "pig"
"""
id(s)
Out[18]: 4355286776
"""
s = "cat"
"""
id(s)
Out[20]: 4324273488
"""
# 字符串赋值
a = "apple"
b = "apple"
a == b
# Out[13]: True
a is b
# Out[12]: True
id(a)...
# 字符串是不可变类型
s = "pig"
"""
id(s)
Out[18]: 4355286776
"""
s = "cat"
"""
id(s)
Out[20]: 4324273488
"""
# 字符串赋值
a = "apple"
b = "apple"
a == b
# Out[13]: True
a is b
# Out[12]: True
id(a)
# Out[14]: 4368069552
id(b)
# Out[15]: 4368069552
"""
a = "red apple"
b = "red apple"
"""
a == b
Out[7]: True
a is b
Out[8]: False
id(a)
Out[5]: 4368267888
id(b)
Out[6]: 4368267696
# 小整数
a = 256
b = 256
"""
a == b
Out[4]: True
a is b
Out[5]: True
id(a)
Out[13]: 4297652576
id(b)
Out[14]: 4297652576
"""
a = 257
b = 257
"""
a == b
Out[8]: True
a is b
Out[9]: False
id(a)
Out[10]: 4367096688
id(b)
Out[11]: 4367097168
# 2 ** 8 = 256
"""
# 元组
t = (0, [])
t[-1].append(1)
"""
t
Out[23]: (0, [1])
"""
t[-1] += [1]
"""
TypeError: 'tuple' object does not support item assignment
t
Out[25]: (0, [1, 1])
"""
t[-1] = t[-1] + [2]
"""
TypeError: 'tuple' object does not support item assignment
t
Out[28]: (0, [1, 1])
[1] + [2]
Out[27]: [1, 2]
"""
# 字典的键
{(1, -1): "value"} # ok
{(1, -1, []): "value"} # error
# 列表复制
import copy
# 赋值
l1 = [1, 2, 3, []]
l2 = l1
l2.append(4)
"""
l1
Out[30]: [1, 2, 3, [], 4]
l2
Out[31]: [1, 2, 3, [], 4]
"""
# 浅拷贝
l1 = [1, 2, 3, []]
l3 = copy.copy(l1)
l3.append(5)
"""
l1
Out[36]: [1, 2, 3, []]
l3
Out[37]: [1, 2, 3, [], 5]
"""
l3[3].append(6)
"""
l1
Out[39]: [1, 2, 3, [6]]
l3
Out[40]: [1, 2, 3, [6], 5]
"""
# 深拷贝
l1 = [1, 2, 3, []]
l4 = copy.deepcopy(l1)
l4[-1].append(7)
"""
l1
Out[42]: [1, 2, 3, []]
l4
Out[43]: [1, 2, 3, [7]]
"""
# 复制列表
l1 = [1, 2, 3]
l2 = l1
l3 = l1[:] # 推荐
"""
id(l1)
Out[45]: 4367794824
id(l2)
Out[46]: 4367794824
id(l3)
Out[47]: 4367785032
"""
# 元组复制
t1 = (1, 2, 3)
t2 = t1
"""
t1 is t2
Out[48]: True
"""
# 二维矩阵
l = [0, 0, 0] * 3
"""
l
Out[50]: [0, 0, 0, 0, 0, 0, 0, 0, 0]
"""
matrix = [[0, 0, 0]] * 3 # 会引用同一个列表
"""
matrix
Out[52]: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
matrix[0][0]=1
matrix
Out[54]: [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
"""
matrix = [[0, 0, 0] for i in range(3)] # 推荐
"""
matrix
Out[56]: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
matrix[0][0]=1
matrix
Out[58]: [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
"""
# None比较
a = None
b = None
"""
a == b
Out[20]: True
a is b
Out[19]: True
id(a)
Out[21]: 4297328656
id(b)
Out[22]: 4297328656
"""
a = 1
a is None # is判断id
# Out[60]: False
a = None
a is None
# Out[62]: True
# 排序
s = [1, 3, 2]
s.sort() # return None 原地操作
s
# Out[65]: [1, 2, 3]
sorted(s)
# Out[66]: [1, 2, 3] # 返回新列表
sorted((1, 2, 3)) # 可迭代即可
# Out[68]: [1, 2, 3]
tuple(1, 2, 3)
# TypeError: tuple() takes at most 1 argument (3 given)
# 检查a is b 的时候,其实相当于检查 id(a) == id(b)。
# 检查 a == b 的时候,实际是调用了对象 a 的 __eq()__ 方法,a == b 相当于 a.__eq__(b)
# python列表可以存不同对象,其实是只保存了对象的地址,地址的大小是固定的
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/81215545
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)