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

时间:2018-03-02 23:21:48

标签: python bash grep

自从我上次使用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。

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

第一个论点:

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

对于第三个论点:

print(decode_predictions(preds, top=3)[0][0][2])
然而,我想要打印两次而不必两次调用decode_predictions(preds, top=3)功能。谢谢你的帮助。

相关问题