标签:创建 反转 sig err 复制 排列 使用 下标索引 xtend
len(list)
list.count(item)
list.index(item)
list.append(item)
list1.extend(list2)
list.insert(index, item)
a, b = [1, 2], [1, 2]
a.append(‘cd‘)
print(a) # [1, 2, ‘cd‘]
b.extend(‘cd‘)
print(b) # [1, 2, ‘c‘, ‘d‘]
list[index] = item
list.sort(reverse=True/False)
(默认为False,升序; 为True时表示降序排列)list.reverse()
a, b = [1, 3, 2, 4], [1, 3, 2, 4]
a.sort()
print(a) # [1, 2, 3, 4]
b.reverse()
print(b) # [4, 2, 3, 1]
del list
(删除指向列表的变量)del list[index]
or list.pop(index)
list.pop()
list.clear()
list.remove(item)
a = [1, 2]
b = [2, 1]
c = a + b # [1, 2, 2, 1]
d = a*3 # [1, 2, 1, 2, 1, 2]
print(a < b) # True
print(a < b and a < c) # True
a = list.copy()
or a = list[:]
a = list
a = [1, 2]
b = a.copy()
c = a[:]
d = a
print(id(a) == id(b)) # False 深拷贝
print(id(a) == id(c)) # False 深拷贝
print(id(a) == id(d)) # True 浅拷贝
python中元组与列表不同之处:元组的元素不能修改
tup = (1, 2)
tup[0] = 2 # TypeError: ‘tuple‘ object does not support item assignment
tup1 = (1, 2)
tup2 = 1, 2 # (1, 2)
tup3 = 1, # (1,)
tup4 = (1,) # (1,) 元组中只包含一个元素时,需在元素后添加","消除歧义
tup5 = (1) # 1
tup6 = () # () 空元组
元组可以使用下标索引来访问元组中的值
tup = (1, 2, 3)
print(tup[0]) # 1
print(tup[1:3]) # (2, 3)
元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组
tup = (1, 2, 3)
del tup[0] # TypeError: ‘tuple‘ object doesn‘t support item deletion
del tup
a, b = (1, 2), (2, 1)
c = a + b # (1, 2, 2, 1)
d = a*3 # (1, 2, 1, 2, 1, 2)
print(a < b) # True
print(a < b or a == b) # True
标签:创建 反转 sig err 复制 排列 使用 下标索引 xtend
原文地址:https://www.cnblogs.com/ericness/p/12680677.html