使用grep获取“”之间的多个单词并对其进行操作

2024-09-27 00:14:43 发布

您现在位置:Python中文网/ 问答频道 /正文

自从上次使用grep已经有一段时间了,我需要一些帮助

这就是我要做的。对于输出,如下所示:

('Predicted:', [(u'n02504458', u'African_elephant', 0.99588591), (u'n01871265', u'tusker', 0.004068926), (u'n02504013', u'Indian_elephant', 4.499541e-05)])

我想grep三个变量n0250458,非洲象,和0.99588591。我还想将0.99588591存储为double以检查值,并以某种方式编辑一个python脚本以包含n0250458

我知道这似乎是很多,但任何帮助是非常感谢


Tags: 脚本编辑方式grepindiandoubleelephantafrican
2条回答
$ python
Python 2.7.14 (default, Jan  5 2018, 10:41:29) 
[GCC 7.2.1 20171224] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = ('Predicted:', [(u'n02504458', u'African_elephant', 0.99588591), (u'n01871265', u'tusker', 0.004068926), (u'n02504013', u'Indian_elephant', 4.499541e-05)])
>>> print(x[1][0])
(u'n02504458', u'African_elephant', 0.99588591)

编辑:

>>> print(x[1][0][2])
0.99588591

https://docs.python.org/2/tutorial/datastructures.html

我想我刚刚明白了。这是我得到第一个论点和第三个论点所做的

对于第一个参数:

print(decode_predictions(preds, top=3)[0][0][0])

对于第三个参数:

print(decode_predictions(preds, top=3)[0][0][2])

不过,我希望打印这两个函数,而不必调用decode_predictions(preds, top=3)函数两次。谢谢你的帮助

相关问题 更多 >

    热门问题