Python列表&元组&拷贝

文章目录

列表

####列表
###定义 lst = list(iterable)
##列表中的元素可以不是同一种数据结构
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
print("lst1:{}".format(lst1))
print("lst2:{}".format(lst2))



###方法
##增
# lst.append(object) 尾部追加,返回值为None,原地修改
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
lst1.append(lst2)
print("lst1.append(lst2):{}".format(lst1))

# lst.extend(iterable) 尾部追加,扩展列表,返回值为None,原地修改
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
lst1.extend(lst2)
print("lst1.extend(lst2):{}".format(lst1))

# lst.insert(index,object) 在index位置插入object,超过上界尾插,超过下界头插,返回值为None,原地修改
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
lst1.insert(0, "abc")
print("lst1.insert(0,\"abc\"):{}".format(lst1))

# * + 生成新列表
lst1 = [1, 2, 3, 4, 5]
lst2 = ["x", "y", "z"]
print("lst1 + lst2 = {}".format(lst1 + lst2))
print("lst2 * 2 = {}".format(lst2 * 2))


##删
# lst.clear() 清空列表
lst1 = [1, 2, 3, 4, 5]
print("lst1.clear():{}".format(lst1.clear()))

# lst.remove(value) 从左到右移除第一个value,就地修改,按值移除
lst1 = [1, 2, 3, 4, 5]
lst1.remove(5)
print("lst1.remove(5):{}".format(lst1))

# lst.pop([index]) 弹出index处的值,如果无index,弹出列表尾的值,返回被弹出的值,按索引移除
lst1 = [1, 2, 3, 4, 5]
a = lst1.pop(3)
print("lst1.pop(3):{},{}".format(lst1, a))
b = lst1.pop()
print("lst1.pop():{},{}".format(lst1, b))


##改
# lst.reverse() 反转列表,返回值为None,原地修改
lst1 = [1, 2, 3, 4, 5]
lst1.reverse()
print("lst1.reverse():{}".format(lst1))

# lst.sort(key=None,reverse=False) 对元素排序,默认升序,返回值为None,原地修改,reverse=True,降序,key为函数,按函数修改
lst3 = [3, 2, 5, 1, 4]
lst3.sort(key=None, reverse=False)
print("lst3.sort(key=None,reverse=False):{}".format(lst3))
lst3 = [3, 2, 5, 1, 4]
lst1.sort(key=None, reverse=True)
print("lst3.sort(key=None,reverse=True):{}".format(lst3))


##查
# lst.count(x) 返回值为整型,返回列表中x的出现次数
lst4 = [1, 2, 3, 3, 3, 4, 5]
print("lst4.count(3):{}".format(lst4.count(3)))

# lst[begin:end:step] 在[begin,end)中以步长为step切片数据
lst4 = [1, 2, 3, 3, 3, 4, 5]
print("lst4[2:5]:{}".format(lst4[2:5]))

# lst.index(obj,begin,end) 在[begin,end)中查找第一个obj出现的索引,找不到报错 ValueError
lst4 = [1, 2, 3, 3, 3, 4, 5]
print("lst4.index(3):{}".format(lst4.index(3)))
# print("lst4.index(7):{}".format(lst4.index(7)))  ValueError: 7 is not in list
---------------------------------------
lst1:[1, 2, 3, 4, 5]
lst2:['x', 'y', 'z']
lst1.append(lst2):[1, 2, 3, 4, 5, ['x', 'y', 'z']]
lst1.extend(lst2):[1, 2, 3, 4, 5, 'x', 'y', 'z']
lst1.insert(0,"abc"):['abc', 1, 2, 3, 4, 5]
lst1 + lst2 = [1, 2, 3, 4, 5, 'x', 'y', 'z']
lst2 * 2 = ['x', 'y', 'z', 'x', 'y', 'z']
lst1.clear():None
lst1.remove(5):[1, 2, 3, 4]
lst1.pop(3):[1, 2, 3, 5],4
lst1.pop():[1, 2, 3],5
lst1.reverse():[5, 4, 3, 2, 1]
lst3.sort(key=None,reverse=False):[1, 2, 3, 4, 5]
lst3.sort(key=None,reverse=True):[3, 2, 5, 1, 4]
lst4.count(3):3
lst4[2:5]:[3, 3, 3]
lst4.index(3):2

元组

####元组
###定义
tup1 = (1, 2, 3, 4, 5, 6)


###方法
## 查
# tup.index(value[,start[,end]]) 返回在区间中值为value的索引,匹配不到报 value error错误
print("tup1.index(3):{}".format(tup1.index(3)))

# tup[::] 切片取值
print("tup1[1:5]:{}".format(tup1[1:5]))

###注意
# 元组是不可变类型
-------------------------------
tup1.index(3):2
tup1[1:5]:(2, 3, 4, 5)

拷贝

####拷贝
###python自带的copy(),只能拷贝了第一层的索引节点
print("~~~lst1=lst.copy()~~~")
lst = [1, 2, 3, 4, ["x", "y", "z"]]
lst1 = lst.copy()
print("lst:{}".format(lst))
print("lst1:{}".format(lst1))
lst[4][0] = "a"
lst.insert(0, 7)
print("after changes lst:{}".format(lst))
print("after changes lst1:{}".format(lst1))


###copy.copy() make a shallow copy,只能拷贝了第一层的索引节点,相当于自带的copy()
print("~~~lst1 = copy.copy(lst)~~~")
lst = [1, 2, 3, 4, ["x", "y", "z"]]
lst1 = copy.copy(lst)
print("lst:{}".format(lst))
print("lst1:{}".format(lst1))
lst[4][0] = "a"
lst.insert(0, 7)
print("after changes lst:{}".format(lst))
print("after changes lst1:{}".format(lst1))

###copy.deepcopy() make a deep copy,完全是另一个对象
print("~~~lst1 = copy.deepcopy(lst)~~~")
lst = [1, 2, 3, 4, ["x", "y", "z"]]
lst1 = copy.deepcopy(lst)
print("lst:{}".format(lst))
print("lst1:{}".format(lst1))
lst[4][0] = "a"
lst.insert(0, 7)
print("after changes lst:{}".format(lst))
print("after changes lst1:{}".format(lst1))
-------------------------------------
~~~lst1=lst.copy()~~~
lst:[1, 2, 3, 4, ['x', 'y', 'z']]
lst1:[1, 2, 3, 4, ['x', 'y', 'z']]
after changes lst:[7, 1, 2, 3, 4, ['a', 'y', 'z']]
after changes lst1:[1, 2, 3, 4, ['a', 'y', 'z']]
~~~lst1 = copy.copy(lst)~~~
lst:[1, 2, 3, 4, ['x', 'y', 'z']]
lst1:[1, 2, 3, 4, ['x', 'y', 'z']]
after changes lst:[7, 1, 2, 3, 4, ['a', 'y', 'z']]
after changes lst1:[1, 2, 3, 4, ['a', 'y', 'z']]
~~~lst1 = copy.deepcopy(lst)~~~
lst:[1, 2, 3, 4, ['x', 'y', 'z']]
lst1:[1, 2, 3, 4, ['x', 'y', 'z']]
after changes lst:[7, 1, 2, 3, 4, ['a', 'y', 'z']]
after changes lst1:[1, 2, 3, 4, ['x', 'y', 'z']]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
列表Python 列表底层实现是一个数组结构,数组中的每个元素都是一个指针,指向实际存储的元素对象。因为 Python 列表的长度是可变的,所以在插入或删除元素时,Python 会重新分配内存,将原有的元素拷贝到新的内存中,以实现动态扩容或缩容。 元组Python 元组底层实现与列表类似,也是一个数组结构,但是元组是不可变的,即一旦创建,就不能再改变其元素的值。因此,Python 不需要为元组提供动态扩容或缩容的支持,这使得元组在某些场景下比列表更加高效。 字典: Python 字典底层实现是一个哈希表,哈希表中的每个元素都包含一个键和一个值。Python 使用哈希函数将键转换为哈希值,然后使用哈希值作为索引,将值存储在哈希表中。当需要查找一个键对应的值时,Python 使用哈希函数将键转换为哈希值,然后在哈希表中查找对应的值。如果存在多个键的哈希值相同,Python 会使用链表将这些键值对连接在一起,称为哈希冲突。 集合: Python 集合底层实现是一个哈希表,类似于字典。Python 集合中的每个元素都是一个键,而值则为 None。集合使用哈希表来存储键,因此集合中的元素是无序的。当需要检查一个元素是否存在于集合中时,Python 使用哈希函数将元素转换为哈希值,然后在哈希表中查找对应的键。如果键存在,则说明元素存在于集合中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值