将浮点值添加到元组
我从视觉工具获取 xy 坐标作为文本(字符串)。我需要添加一个常量值 到该字符串中的每个 x - 元素。
通过这些陈述,可以获得原始值和我的愿景的命令的元组- 工具需要一个元组作为参数,
above=GetValue('ObererBogen.FittedPoints')
above=above.replace('(((','((')
above=above.replace('),),)','))')
above=eval(above)
现在我有一个上面命名的带有 x,y 坐标的元组。 但是如何为每个 x 值添加一个常量值呢?
I get x y coordinates from a vision tool as text(string). I need to add a constant value
to every x - element in this string.
with this statements a get a tuple of the original values and the command of my vision-
tool needs a tuple as argument
above=GetValue('ObererBogen.FittedPoints')
above=above.replace('(((','((')
above=above.replace('),),)','))')
above=eval(above)
now I have a tuple named above with x,y-coordinates.
But how add a constant value to each x-value ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最优雅的方法可能是使用
numpy
。我将添加和转换为
tuple()
步骤分开只是为了展示,但如果您实际上这样做,您可以只执行tuple(np.add(tuple1, tuple2))
。否则,你就必须做一些笨重的事情,比如
a = (a[0] + 5, a[1])
(出于各种原因,我一直不喜欢这样做,尽管从技术上讲这是最简单的解决方案),或者将tuple
类型进行子类化,以创建一种将加法应用于元素而不是串联的类型,或者使用collections.namedtuple
工厂和使用它创建的类实例的_replace
方法。The most elegant way would probably be to use
numpy
.I separated the adding and converting to
tuple()
steps just to show, but if you were actually doing it you could just dotuple(np.add(tuple1, tuple2))
.Otherwise, you'd have to do something clunky like
a = (a[0] + 5, a[1])
(which I've always disliked for a variety of reasons, despite this technically being the easiest solution), or either subclass thetuple
type to make a type where addition applied to the elements rather than being concatenation or use thecollections.namedtuple
factory and the_replace
method of instances of the class created with it.