Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 调用函数并在下一次运行中使用后如何存储输出_Python_Python 2.7 - Fatal编程技术网

Python 调用函数并在下一次运行中使用后如何存储输出

Python 调用函数并在下一次运行中使用后如何存储输出,python,python-2.7,Python,Python 2.7,我在解释器中运行了下面的代码,并调用了union函数 快速查找(10)。联合(3,4) 输出:[0,1,2,4,4,5,6,7,8,9] 快速查找(10)。联合(0,4) 输出:[4,1,2,3,4,5,6,7,8,9] 当我第二次调用union函数时,输出列表应该是这样的 [4,1,2,4,4,5,6,7,8,9] 但是它却给了我[4,1,2,3,4,5,6,7,8,9]作为输出。我怎样才能得到我想要的输出。请建议 class quick_find: def __init__(sel

我在解释器中运行了下面的代码,并调用了union函数

快速查找(10)。联合(3,4)

输出:
[0,1,2,4,4,5,6,7,8,9]

快速查找(10)。联合(0,4)

输出:
[4,1,2,3,4,5,6,7,8,9]

当我第二次调用union函数时,输出列表应该是这样的
[4,1,2,4,4,5,6,7,8,9]

但是它却给了我
[4,1,2,3,4,5,6,7,8,9]
作为输出。我怎样才能得到我想要的输出。请建议

class quick_find:

    def __init__(self,n):
        self.id = [number for number in xrange(n)]


    def union(self,x,y):
        j = 0
        elements = self.id
        for i in elements:
            if i == elements[x]:
                elements[j] = elements[y]
            j = j+1

        self.id = elements
        return elements 

通过不将列表分配给任何“占位符”对象/变量,可以创建所请求列表的新实例。像这样做,你会保持你的列表的机智

myInstance = quick_find(10)
print(myInstance.union(0,4))
print(myInstance.union(3,4))
你现在实际做的是

myInstance = quick_find(10)
print(myInstance.union(0,4))

mySecondInstance = quick_find(10)
print(mySecondInstance.union(3,4))

…显然,这不是你想要的方式;)

您正在创建所请求列表的新实例,方法是不将其分配给任何“占位符”对象/变量。像这样做,你会保持你的列表的机智

myInstance = quick_find(10)
print(myInstance.union(0,4))
print(myInstance.union(3,4))
你现在实际做的是

myInstance = quick_find(10)
print(myInstance.union(0,4))

mySecondInstance = quick_find(10)
print(mySecondInstance.union(3,4))

…显然,这不是你想要的方式;)

您实际上每次都在对新实例调用
union()
方法:

代码的改进版本:

class Quick_find:
    def __init__(self,n):
        self.id = range(n)    #just range() is enough

    def union(self,x,y):
        for i,elem in enumerate(self.id):    #use enumerate() for indexes
            if elem==x:
                self.id[i]=y

    def show(self):
        print self.id

q=Quick_find(10)       #create a instance
q.union(3,4)           #call union on that instance
q.union(0,4)           #call union on that instance
q.show()               
输出:

[4, 1, 2, 4, 4, 5, 6, 7, 8, 9]

实际上,您每次都在对新实例调用
union()
方法:

代码的改进版本:

class Quick_find:
    def __init__(self,n):
        self.id = range(n)    #just range() is enough

    def union(self,x,y):
        for i,elem in enumerate(self.id):    #use enumerate() for indexes
            if elem==x:
                self.id[i]=y

    def show(self):
        print self.id

q=Quick_find(10)       #create a instance
q.union(3,4)           #call union on that instance
q.union(0,4)           #call union on that instance
q.show()               
输出:

[4, 1, 2, 4, 4, 5, 6, 7, 8, 9]

谢谢Allendar理解问题和解决方案。谢谢Allendar理解问题和解决方案。@user2229096如果对你有效,你可以。答案对我有效。。。感谢代码中的建议@ashwini@user2229096如果对你有用的话你可以。答案对我有用。。。感谢代码@ashwini中的建议