ROS Answers: Open Source Q&A Forum - RSS feedhttps://answers.ros.org/questions/Open source question and answer forum written in Python and DjangoenROS Answers is licensed under Creative Commons Attribution 3.0Wed, 07 Sep 2016 01:28:01 -0500why i cannot use intersection with position listhttps://answers.ros.org/question/243180/why-i-cannot-use-intersection-with-position-list/hi guys, i try to find a intersection set of two position lists
so i write a code in python, and i have two lists, such as :
position1 = Point()
position1.x =1
position2 = Point()
position2.x=2
a = [copy.deepcopy(position1),copy.deepcopy(position2)]
b = [copy.deepcopy(position1)]
then, when i try to get intersection of those two list a and b
it return me an answer: set([])
that's ridiculous,
normally i should have an answer like: set(a).intersection(set(b)) = set([position1])
if anyone could help me to fix this problem?
it's great thankful for viewing this problem
and i appreciate for your watching and answering.
thanks in advance.
Tue, 06 Sep 2016 04:55:59 -0500https://answers.ros.org/question/243180/why-i-cannot-use-intersection-with-position-list/Answer by DinnerHowe for <p>hi guys, i try to find a intersection set of two position lists</p>
<p>so i write a code in python, and i have two lists, such as :</p>
<pre><code>position1 = Point()
position1.x =1
position2 = Point()
position2.x=2
a = [copy.deepcopy(position1),copy.deepcopy(position2)]
b = [copy.deepcopy(position1)]
</code></pre>
<p>then, when i try to get intersection of those two list a and b</p>
<p>it return me an answer: set([])</p>
<p>that's ridiculous,</p>
<p>normally i should have an answer like: set(a).intersection(set(b)) = set([position1])</p>
<p>if anyone could help me to fix this problem? </p>
<p>it's great thankful for viewing this problem</p>
<p>and i appreciate for your watching and answering.</p>
<p>thanks in advance.</p>
https://answers.ros.org/question/243180/why-i-cannot-use-intersection-with-position-list/?answer=243240#post-id-243240hi, guys i figure it out ... in ros when we initial the parameters, like position2 = Point() and deepcopy, the hash value is change. however, intersection method compare both hash value and it's value, so what we can do is build a new class just compare value without hash value. OR the other method is rebuild the hash value by yourself, such as Ninja Puppy's suggestion:
class NewPoint(Point):
def __hash__(self):
return hash(self.x) # maybe self.y as well?
Wed, 07 Sep 2016 01:28:01 -0500https://answers.ros.org/question/243180/why-i-cannot-use-intersection-with-position-list/?answer=243240#post-id-243240