美好的事可不可以发生在我身上? ------ 博客首页

关于深拷贝和浅拷贝【腾讯python面试必考题】

# 1/深拷贝对不可变里边嵌套了可变的,会先对可变进行空间重新开辟,在对外边儿的不可变也进行空间重新开辟
# 1/如果不可变里边嵌套的也是不可变,就不会开辟新空间.
# 2/对可变类型,会对任何一层可变类型数据都进行空间重新开辟.

# 3/浅拷贝对可变类型数据,只对第一层数据进行空间重新开始开辟
# 3/浅拷贝对不可变类型数据,第一层都不会重新开辟,只是多加一个指针


a = [[1, 2, 3], [4, 5, 6]]
b = a
c = copy.copy(a)
d = copy.deepcopy(a)
a.append(7)
a[1][2] = 0

print('愿列表:', a)
print('引用赋值:', b)
print('浅拷贝:', c)
print('深拷贝:', d)
'''
愿列表: [[1, 2, 3], [4, 5, 0], 7]
引用赋值: [[1, 2, 3], [4, 5, 0], 7]
浅拷贝: [[1, 2, 3], [4, 5, 0]]
深拷贝: [[1, 2, 3], [4, 5, 6]]
'''
# 拷贝到即开辟地址,没拷贝到即直接引用。开辟地址不会因原空间数据变化而变化,引用会因原空间数据变化而变化
# =赋值即是全部引用。
posted @ 2022-01-04 17:16  Leshol  阅读(78)  评论(0)    收藏  举报
style="position: fixed; left: 0px; top: 0px; z-index: 2147483647; pointer-events: none;"> var a_idx = 0; jQuery(document).ready(function ($) {undefined $("body").click(function (e) {undefined var a = new Array( "❤富强❤", "❤民主❤", "❤和谐❤", "❤文明❤", "❤自由❤", "❤平等❤", "❤公正❤", "❤法治❤", "❤爱国❤", "❤敬业❤", "❤诚信❤", "❤友善❤", ); var $i = $("").text(a[a_idx]); a_idx = (a_idx + 1) % a.length; var x = e.pageX, y = e.pageY; $i.css({undefined "z-index": 999999999999999999999999999999999999999999999999999999999999999999999, "top": y - 20, "left": x, "position": "absolute", "font-weight": "bold", "color": "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")" }); $("body").append($i); $i.animate({undefined "top": y - 180, "opacity": 0 }, 1500, function () {undefined $i.remove(); }); }); }); (function ($) {undefined $.fn.snow = function (options) {undefined var $flake = $(' documentHeight = $(document).height(), documentWidth = $(document).width(), defaults = {undefined minSize: 10, maxSize: 20, newOn: 1000, flakeColor: "#AFDAEF" /* 此处可以定义雪花颜色,若要白色可以改为#FFFFFF */ }, options = $.extend({}, defaults, options); var interval = setInterval(function () {undefined var startPositionLeft = Math.random() * documentWidth - 100, startOpacity = 0.5 + Math.random(), sizeFlake = options.minSize + Math.random() * options.maxSize, endPositionTop = documentHeight - 200, endPositionLeft = startPositionLeft - 500 + Math.random() * 500, durationFall = documentHeight * 10 + Math.random() * 5000; $flake.clone().appendTo('body').css({undefined left: startPositionLeft, opacity: startOpacity, 'font-size': sizeFlake, color: options.flakeColor }).animate({undefined top: endPositionTop, left: endPositionLeft, opacity: 0.2 }, durationFall, 'linear', function () {undefined $(this).remove() }); }, options.newOn); }; })(jQuery); $(function () {undefined $.fn.snow({undefined minSize: 5, /* 定义雪花最小尺寸 */ maxSize: 50,/* 定义雪花最大尺寸 */ newOn: 300 /* 定义密集程度,数字越小越密集 */ }); });