不能使用 python 交集与 ROS 点列表
cannot use python intersection set with ROS point lists
我尝试在 ROS 中找到两个位置列表的交集
所以我在python中写了一个代码,我有两个列表,比如:
position1 = Point()
position1.x =1
position2 = Point()
position2.x=2
a = [copy.deepcopy(position1),copy.deepcopy(position2)]
b = [copy.deepcopy(position1)]
然后,当我尝试获取这两个列表 a 和 b 的交集时
它return我一个答案:set([])
太荒谬了,
通常我应该有这样的答案:set(a).intersection(set(b)) = set([position1])
是否有人可以帮我解决这个问题?
非常感谢看到这个问题
感谢您的观看和回答。
提前致谢。
这是我的测试代码
import rospy,copy
from geometry_msgs.msg import Point
class test():
def __init__(self):
position1 = Point()
position1.x =1
position2 = Point()
position2.x=2
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__':
try:
rospy.loginfo ("initialization system")
test()
rospy.loginfo ("process done and quit")
except rospy.ROSInterruptException:
rospy.loginfo("robot twist node terminated.")
顺便说一句,ROS 点类型发布在这里:http://docs.ros.org/jade/api/geometry_msgs/html/msg/Point.html
好吧,它不会 returns set([])
而是打印 set()
(可能吹毛求疵,但它不是一回事)。
实际上这是预期的行为:
- 集合只保留唯一元素
- 唯一性是通过具有相同的散列来定义的
- 默认哈希来自 hash() 方法
- hash() 使用对象的id
- 你深度复制了你的对象,所以它们有不同的 ID
- 所以他们是不同的
我不知道为什么你认为你需要 deepcopy 但如果你将你的点直接放在列表中,它会按预期工作
@Ninja Puppy 删了一个回答,我觉得也有帮助,所以加在这里。同时,如果这是被禁止的或者 Ninja Puppy 对此感到厌烦,我也会删除这个答案。以下是他的回答
Okay, going by your comment:
a[0]==b[0] is true, because they are both position1, but hash(a[0]) and hash(b[0]) is different
One way is to create a new Point that supports a hash that's
meaningful for use in a set, eg:
class NewPoint(Point):
def __hash__(self):
return hash(self.x) # maybe self.y as well?
Ideally your hash should include the same attributes that
Point.__eq__
uses.
Then use NewPoint to construct your objects instead.
我尝试在 ROS 中找到两个位置列表的交集
所以我在python中写了一个代码,我有两个列表,比如:
position1 = Point()
position1.x =1
position2 = Point()
position2.x=2
a = [copy.deepcopy(position1),copy.deepcopy(position2)]
b = [copy.deepcopy(position1)]
然后,当我尝试获取这两个列表 a 和 b 的交集时
它return我一个答案:set([])
太荒谬了,
通常我应该有这样的答案:set(a).intersection(set(b)) = set([position1])
是否有人可以帮我解决这个问题?
非常感谢看到这个问题
感谢您的观看和回答。
提前致谢。
这是我的测试代码
import rospy,copy
from geometry_msgs.msg import Point
class test():
def __init__(self):
position1 = Point()
position1.x =1
position2 = Point()
position2.x=2
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__':
try:
rospy.loginfo ("initialization system")
test()
rospy.loginfo ("process done and quit")
except rospy.ROSInterruptException:
rospy.loginfo("robot twist node terminated.")
顺便说一句,ROS 点类型发布在这里:http://docs.ros.org/jade/api/geometry_msgs/html/msg/Point.html
好吧,它不会 returns set([])
而是打印 set()
(可能吹毛求疵,但它不是一回事)。
实际上这是预期的行为:
- 集合只保留唯一元素
- 唯一性是通过具有相同的散列来定义的
- 默认哈希来自 hash() 方法
- hash() 使用对象的id
- 你深度复制了你的对象,所以它们有不同的 ID
- 所以他们是不同的
我不知道为什么你认为你需要 deepcopy 但如果你将你的点直接放在列表中,它会按预期工作
@Ninja Puppy 删了一个回答,我觉得也有帮助,所以加在这里。同时,如果这是被禁止的或者 Ninja Puppy 对此感到厌烦,我也会删除这个答案。以下是他的回答
Okay, going by your comment:
a[0]==b[0] is true, because they are both position1, but hash(a[0]) and hash(b[0]) is different
One way is to create a new Point that supports a hash that's meaningful for use in a set, eg:
class NewPoint(Point): def __hash__(self): return hash(self.x) # maybe self.y as well?
Ideally your hash should include the same attributes that
Point.__eq__
uses.Then use NewPoint to construct your objects instead.