python 的深浅拷贝问题

深浅拷贝概念

基本类型和引用类型数据拷贝的问题。因为基本类型的数据大小是固定的,所以他保存在栈内存中;而引用类型的数据大小不固定,因而保存在堆内存中,单引用类型在栈内存中只保存一个指向堆内存的指针。
浅拷贝:对于浅拷贝来说,如果拷贝基本类型,那么就等于赋值一样,会直接拷贝其本身;但如果拷贝的是引用类型,就只会拷贝一层,如果 原对象发生改变,那么拷贝对象也会发生改变。
深拷贝:深拷贝的话就会拷贝多层,嵌套的对象也会被拷贝出来,相当于开辟一个新的内存地址用于存放拷贝的对象。
在python语言中没有明显的指出,例如,操作指针或对于指针的操作。
在java或者go中是可以的。

业务需求

抓取交易所黄金的k线,进行数据分析。

问题代码

import datetime


def judgment(info):
    minute = datetime.timedelta(minutes=15)
    count = 0
    while True:
        Long = len(info)
        if count + 1 == Long:
            break
        if info[count]["TS"] + minute == info[count + 1]["TS"]:
            pass
        else:
            date = info[count]
            date["TS"] = info[count]["TS"] + minute
            info.insert(count + 1, date)
        count += 1
    return info


info = [{'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 2, 45), 'P': '312.10', 'L': '312.10',
         'H': '312.10', 'O': '312.10'},
        {'C': 'AUTD', 'T': '1561338900', 'TS': datetime.datetime(2019, 6, 22, 5, 15), 'P': '313.88', 'L': '313.03',
         'H': '314.97', 'O': '313.30'}]
print(len(info))
date = judgment(info)
print(date)
date1 = [{'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561338900', 'TS': datetime.datetime(2019, 6, 22, 5, 15), 'P': '313.88', 'L': '313.03',
          'H': '314.97', 'O': '313.30'}]

可以发现代码逻辑是没有问题的,问题出在深浅拷贝的问题。按照正常的逻辑他的值会进行替换,而不是出现错误。一个简单的问题弄了一下午,汗颜。

import datetime
import copy


def judgment(info):
    minute = datetime.timedelta(minutes=15)
    count = 0
    info_date = []
    while True:
        Long = len(info)
        if count + 1 == Long:
            break
        if info[count]["TS"] + minute == info[count + 1]["TS"]:
            pass
        else:
            date = info[count]
            date["TS"] = info[count]["TS"] + minute
            info.insert(count + 1, date)
        # print(info[count])
        info_date.append(copy.deepcopy(info[count]))

        count += 1
    return info_date


info = [{'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 2, 45), 'P': '312.10', 'L': '312.10',
         'H': '312.10', 'O': '312.10'},
        {'C': 'AUTD', 'T': '1561338900', 'TS': datetime.datetime(2019, 6, 22, 5, 15), 'P': '313.88', 'L': '313.03',
         'H': '314.97', 'O': '313.30'}]
print(len(info))
date = judgment(info)
print(date)

date1 = [{'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 3, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 3, 15), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 3, 30), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 3, 45), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 4, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 4, 15), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 4, 30), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 4, 45), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'},
         {'C': 'AUTD', 'T': '1561142700', 'TS': datetime.datetime(2019, 6, 22, 5, 0), 'P': '312.10', 'L': '312.10',
          'H': '312.10', 'O': '312.10'}]

总结:Python是没有基础数据类型(primitive value type),全部都是对象.也就意味着变量和属性全部都是引用类型.
为了提醒大家所以讲问题整理出来。在写程序的时候需要注意,深浅拷贝问题,有时候我们往往忽略最简单的问题。

你可能感兴趣的:(python 的深浅拷贝问题)