使用grep在''之间获取多个单词并操纵它们

问题描述 投票:-1回答:2

自从上次我使用grep以来已经有一段时间了,我可以使用一些帮助。

这是我想要做的。如下所示:

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

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

我知道这似乎很多,但任何帮助都非常感激。

python bash grep
2个回答
1
投票
$ 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


0
投票

我想我刚想通了。这是我为获得第一个参数和第三个参数所做的。

对于第一个参数:

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

对于第三个论点:

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

然而,我想要打印两次而不必调用decode_predictions(preds, top=3)函数两次。谢谢您的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.