Python__模块(DATA-辅助-part1)__copy / queue / string

本文介绍了Python中深拷贝与浅拷贝的区别,通过实例展示了如何在多维列表中使用`deepcopy`实现深拷贝,以及浅拷贝的浅层次效果。同时,对队列(FIFO和LIFO)的概念和使用方法进行了讲解,包括Queue和LifoQueue的创建和操作。
摘要由CSDN通过智能技术生成

copy(简介)

对数据进行深拷贝与浅拷贝


copy(参数列表)

deepcopy

深拷贝(多维列表/通用)

copy

浅拷贝(常规列表)


copy(参考代码)

区别(深拷贝 / 浅拷贝 / 赋值)

import copy
""" 浅拷贝 """
numbers = ["one", "two", "three"]
cp1 = copy.copy(numbers)
cp1.append("four")
print("原本:", numbers)     # >>> 原本: ['one', 'two', 'three']
print("复制后:", cp1)       # >>> 复制后: ['one', 'two', 'three', 'four']

""" 深拷贝 """
box = ["one", "two", "three", ["four", ["five", "six"]]]
lis = copy.deepcopy(box)
lis[-1].append("add")
print("原本:", box)          # >>> 原本: ['one', 'two', 'three', ['four', ['five', 'six']]]
print("复制后:", lis)        # >>> 复制后: ['one', 'two', 'three', ['four', ['five', 'six'], 'add']]

""" 赋值 """
test = ["1", "2", "3"]
new = test
new.append("4")
print("常规赋值:", test)    # >>> 常规赋值: ['1', '2', '3', '4']
print("赋值后", new)        # >>> 赋值后 ['1', '2', '3', '4']

queue(简介)

队列:对数据进行顺序排列,先进先出或后进先出的规则。


queue(参考代码)

综合例子

import queue
# FIFO队列-先进先出
"""
q = queue.Queue()
for i in range(5):
    q.put(i)
while not q.empty():
    print(q.get())
"""

# LIFO队列-后进先出
"""
q = queue.LifoQueue()
for i in range(5):
    q.put(i)
while not q.empty():
    print(q.get())
"""

string(参数列表)

string.ascii_letters

包含a-Z的所有大小写字母

string.digits

包含0~9的所有数字


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vip飞梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值