从 Jython 中的数据中仅提取数字

发布于 2024-07-26 16:03:13 字数 217 浏览 0 评论 0原文

这是我的问题。 我正在开发一个 Jython 程序,我必须从 PyJavaInstance 中提取数字:

[{string1="foo", xxx1, xxx2, ..., xxxN, string2="bar"}]

(其中 xxx 是浮点数数字)。

我的问题是如何提取数字并将它们放入更简单的结构(如 python 列表)中。

先感谢您。

Here is my problem. I'm working on a Jython program and I have to extract numbers from a PyJavaInstance:

[{string1="foo", xxx1, xxx2, ..., xxxN, string2="bar"}]

(where xxx are the floating point numbers).

My question is how can I extract the numbers and put them in a more simple structure like a python list.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不离久伴 2024-08-02 16:03:13

PyJavaInstance 是 Java 实例的 Jython 包装器; 如何从中提取数字取决于它是什么。 如果您需要获取一堆东西 - 其中一些是字符串,一些是浮点数,那么:

float_list = []
for item in instance_properties:
    try:
        float_list.append(float(item))
    except ValueError:
        pass

A PyJavaInstance is a Jython wrapper around a Java instance; how you extract numbers from it depends on what it is. If you need to get a bunch of stuff - some of which are strings and some of which are floats, then:

float_list = []
for item in instance_properties:
    try:
        float_list.append(float(item))
    except ValueError:
        pass
转身泪倾城 2024-08-02 16:03:13

你能迭代并检查一个项目是否是浮动的吗? 您正在寻找的方法是 isinstance 。 我希望它能在 Jython 中实现。

can you iterate and check whether an item is float? The method you're looking for is isinstance. I hope it's implemented in Jython.

趁年轻赶紧闹 2024-08-02 16:03:13

谢谢维奈。 这也是我刚刚找到的解决方案:

 new_inst=[]
for element in instance:
    try:
        float(element)
        new_inst.append(float(element))
    except ValueError:
        del(element)

@SilentGhost:很好的建议。 问题是找到什么方法可以确定我迭代的每个元素是否是浮点数。

Thank you Vinay. It's also the kind of solution I've just found:

 new_inst=[]
for element in instance:
    try:
        float(element)
        new_inst.append(float(element))
    except ValueError:
        del(element)

@SilentGhost: Good suggestion. The issue was to find what method could determine if each element I iterate is a float number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文