Python,如何以有效的方式复制对象,允许修改它吗? [英] Python, how to copy an object in an efficient way that permits to modyfing it too?

查看:160
本文介绍了Python,如何以有效的方式复制对象,允许修改它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Python代码我有以下问题:我必须多次复制同一对象,然后将每个副本传递到一个函数,修改它。我试图与copy.deepcopy,但它真的计算昂贵,然后我尝试itertools.repeat(),但这是一个坏主意,因为之后,我必须修改对象。所以我写了一个简单的方法,复制一个对象只需返回一个新的对象具有相同的属性:

  def myCopy(myObj): 
return MyClass(myObj.x,myObj.y)

问题是真的非常有效:我必须让它abaout 6000次,它需要超过10秒!



要复制和修改的对象是表,它是这样创建的:

  def initialState(self):
table = []
[table.append(Events())for _ in xrange(self.numSlots )]
for xi中的ei(self.numEvents - 1):
ei + = 1
enr = self.exams [ei]
k = random.randint .nu​​mSlots - 1)
table [k] .Insert(ei,enr)
x = EtState(table)
return x

类事件:

def __init __(self,i,enrollment,contribution = None):
self.ei = i
self.enrollment = enrollment
self.contribution = contribution



类事件:

def __init __(self):
self.count = 0
self.EventList = []

def getEvent(self,i):
return self.EventList [i] .ei


def getEnrollment(self,i):
return self.EventList [i] .enrollment

def插入(self,ei,enroll = 1,contribution = None):
self.EventList.append )
self.count + = 1

def eventIn(self,ei):
for x in xrange(self.count):
if(self。 EventList [x] .ei == ei):
self.EventList [x] .enrollment + = 1
return True
return False
/ pre>

解决方案

更多Pythonic方法是创建修改对象的函数,对象,只返回其修改后的形式。但是从这个代码,你发布,不清楚你是否真正尝试做,你应该做一个更简单的(通用)例子你想做什么。



因为Python中的Object意味着任何东西,类,实例,字典,列表,元组,'a'等。



复制对象是不清楚的。 ..
如果我理解正确的话,就是一个类的拷贝实例。



所以写一个函数需要一个类的实例,实例并复制所有你需要的属性。


in my Python code I have the following issue: i have to copy the same object many times and then pass each copy to a function that modifies it. I tried with copy.deepcopy, but it's really computationally expensive, then i tried with itertools.repeat(), but it was a bad idea because after that i've to modify the object. So i wrote a simple method that copy an object simply returning a new object with the same attributes:

def myCopy(myObj):
    return MyClass(myObj.x, myObj.y)

The problem is that this is really unefficient too: i've to make it abaout 6000 times and it takes more than 10 seconds! So, does exist a better way to do that?

The object to copy and modify is table, that is created like that:

def initialState(self):
    table = []
    [table.append(Events()) for _ in xrange(self.numSlots)] 
    for ei in xrange(self.numEvents - 1):
        ei += 1
        enr = self.exams[ei]
        k = random.randint(0, self.numSlots - 1)
        table[k].Insert(ei, enr)
    x = EtState(table)
    return x

class Event:

    def __init__(self, i, enrollment, contribution = None):
        self.ei = i
        self.enrollment = enrollment
        self.contribution = contribution



class Events:

    def __init__(self):
        self.count = 0
        self.EventList = []

    def getEvent(self, i):
        return self.EventList[i].ei


    def getEnrollment(self, i):
        return self.EventList[i].enrollment

    def Insert(self, ei, enroll = 1, contribution = None):
        self.EventList.append(Event(ei, enroll, contribution))
        self.count += 1

    def eventIn(self, ei):
        for x in xrange(self.count):
            if(self.EventList[x].ei == ei):
                self.EventList[x].enrollment += 1
                return True
        return False

解决方案

More Pythonic way would be to create function(s) that modify the object, but don't modify the original object, just return its modified form. But from this code you posted, it is not clear what are you acutally trying to do, you should make a more simple (generic) example of what are you trying to do.

Since Object in Python means anything, class, instance, dict, list, tuple, 'a', etc..

to copy object is kind of not clear... You mean copy instance of a Class if I understood it correctly

So write a function that takes one instance of that class, in that function create another instance and copy all atributes you need..

这篇关于Python,如何以有效的方式复制对象,允许修改它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆