Python 使用grep获取'';并操纵它们

Python 使用grep获取'';并操纵它们,python,bash,grep,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三个变量n0250458、非洲象和0.99588591。我还希望将

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

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

$ 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

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

对于第一个论点:

print(decode_predictions(preds, top=3)[0][0][0])
print(decode_predictions(preds, top=3)[0][0][2])
关于第三个论点:

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

但是,我希望打印这两个函数,而不必调用两次
decode\u预测(preds,top=3)
函数。谢谢你的帮助。

这是python代码!Bash在这方面确实是一个错误的工具。Python将能够以本机方式解析这些数据——请参阅标准库中的
ast.literal\u eval()
,以安全地解析这些数据……因此,不要自动编辑Python脚本;相反,编写Python脚本来读取它自己需要的数据。这是Python代码的输出,因此调整代码以准确输出您需要的数据应该更容易,对吗?如何将其余部分分开?如何将N0250458用作另一个函数的字符串变量?另外,如何将0.99588591存储为双精度?问题是输出来自以下命令,我不能简单地在print中打印:print('Predicted:',decode_predictions(preds,top=3)[0])为什么要调用它两次
result=decode\u预测(preds,top=3)[0][0]
在一行上,然后您可以在同一语句中同时引用
result[0]
result[2]
,将它们作为单独的参数传递,或者执行任何需要的操作。哦,太好了。谢谢