[英]Why can't I change the values of a list in the class using a method?
I'm trying to implement simple Quick Find algorithm using python.我正在尝试使用 python 实现简单的快速查找算法。 This is the first time I am using OOP in Python.
这是我第一次在 Python 中使用 OOP。 Following are the steps I took:
以下是我采取的步骤:
Create the class with init method so that it accepts N no.使用 init 方法创建 class 以便它接受 N 号。 of elements for the list - "id", and I append the 0 - N-1 elements to the list.
列表的元素 - “id”,我 append 列表中的 0 - N-1 个元素。
class QuickFindUF: def __init__(self, N): self.id = [] for i in range(N): self.id.append(i)
I create a union method which accepts the arguments: p & q(these are the values that I want to connect) and then change the list values so that the list items which have pid are changed to qid.我创建了一个接受 arguments 的联合方法:p & q(这些是我要连接的值),然后更改列表值,以便将具有 pid 的列表项更改为 qid。
def union(self,p, q): pid = self.id[p] qid = self.id[q] for i in range(len(self.id)): if self.id[i] == pid: # This part is getting ignored, I think. self.id[i] == qid
I create get_id method to see the changes in the id.我创建 get_id 方法来查看 id 的变化。
def get_id(self): return self.id
Now in the main part I do this to see the results:现在在主要部分中,我这样做是为了查看结果:
if __name__ == "__main__":
qf = QuickFindUF(5) print(qf.get_id()) qf.union(0, 3) print(qf.get_id())
I should see the updated id[] after I call the union method but id doesn't change.调用 union 方法后,我应该会看到更新的 id[] ,但 id 不会改变。
Expected output: [0, 1, 2, 3, 4]
[3, 1, 2, 3, 4]
预期 output:
[0, 1, 2, 3, 4]
[3, 1, 2, 3, 4]
Actual output: [0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
实际 output:
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
I tried to change some values of id manually without using the "if" statement in union method and that worked out fine, like: id[0] = 'a'
and that worked out fine: the output was: [0, 1, 2, 3, 4]
['a', 1, 2, 3, 4]
我尝试在不使用联合方法中的“if”语句的情况下手动更改 id 的一些值,结果很好,例如:
id[0] = 'a'
,结果很好:output 是: [0, 1, 2, 3, 4]
['a', 1, 2, 3, 4]
So why is the union method not working if I use a for loop with if statements to change the values of the list?那么,如果我使用带有 if 语句的 for 循环来更改列表的值,为什么 union 方法不起作用?
I also tried returning the id[] in the union() like this:我还尝试在 union() 中返回 id[],如下所示:
def union(self,p, q):
```pid = self.id[p]
qid = self.id[q]
for i in range(len(self.id)):
if self.id[i] == pid: # This part is getting ignored, I think.
self.id[i] == qid```
But I get the same output when I print(qf.union())但是当我打印时我得到相同的 output(qf.union())
Try this尝试这个
def union(self,p, q):
pid = self.id[p]
qid = self.id[q]
for i in range(len(self.id)):
if self.id[i] == pid: # This part is getting ignored, I think.
self.id[i] = qid
I would suggest to use numpy:我建议使用 numpy:
import numpy as np
class QuickFindUF:
def __init__(self, N):
self.id = np.arange(N) # Quicker with numpy
def union(self, p, q):
pid = self.id[p]
qid = self.id[q]
# Use powerful np.where
self.id = np.where(self.id == pid, # Where self.id = pid,
qid, # changes it to qid,
self.id) # the rest of the array remains unchanged
def get_id(self):
return self.id
if __name__ == "__main__":
qf = QuickFindUF(5)
print(qf.get_id())
qf.union(0, 3)
print(qf.get_id())
If I understood your problem correctly, it should work fine.如果我正确理解了您的问题,它应该可以正常工作。 Otherwise adapt the parameters of np.where().
否则调整 np.where() 的参数。
Good luck !祝你好运 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.