HOWE HOWE - 3 months ago 17
Python Question

cannot use python intersection set with ROS point lists

i try to find a intersection set of two position lists, in ROS

so i write a code in python, and i have two lists, such as :

position1 = Point()

position1.x =1

position2 = Point()


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.

here is my testing code

import rospy,copy
from geometry_msgs.msg import Point
class test():
def __init__(self):
position1 = Point()
position1.x =1
position2 = Point()
a = [copy.deepcopy(position1),copy.deepcopy(position2)]
b = [copy.deepcopy(position1)]
print set(a).intersection(set(b))
print 'a', set(a),'\n'
print 'b', set(b)

if __name__=='__main__':
rospy.loginfo ("initialization system")
rospy.loginfo ("process done and quit")
except rospy.ROSInterruptException:
rospy.loginfo("robot twist node terminated.")

BTW,ROS Point type is posted here:


Well, it doesn't returns set([]) but prints set() (nitpicking maybe but it's not the same thing).

Actually it's the expected behavior :

  • a set keeps only unique elements
  • unique is defined by having the same hash
  • the default hash comes from hash() method
  • hash() uses the id of the object
  • you deepcopy your objects so they have different ids
  • so they're different

I don't know why you think you need deepcopy but if you put your points directly in lists, it will works as expected