[英]Filtering tuples inside tuple
I am trying to filter a big number of tuples that look like this one:我正在尝试过滤大量看起来像这样的元组:
tuple = ([], [('print', -108.0), ('denim', -144.0), ('floral', -158.0), ('summer', -172.0), ('chiffon', -217.0), ('crochet', -272.0), ('cotton', -275.0), ('graphic', -279.0), ('pattern', -281.0), ('sheer', -294.0)])
What I am trying to do is to create some lines of code that I can use to say to Python that I want to retrieve a list that contains just the strings with a number that is higher than -200,in this case I would like to obtain:我想做的是创建一些代码行,我可以用来对 Python 说我想检索一个仅包含数字高于 -200 的字符串的列表,在这种情况下,我想获得:
list = ["print", "denim", "floral", "summer"]
NEW PART:新零件:
Now thanks to @Andrej Kesely I have the solution to this problem, but if I want to do the same things but with a list of tuples like this one:现在感谢@Andrej Kesely,我找到了解决这个问题的方法,但是如果我想做同样的事情,但使用一个像这样的元组列表:
predictions = [([],
[('print', -72.0),
('summer', -141.0),
('chiffon', -157.0),
('floral', -266.0),
('graphic', -279.0),
('cotton', -279.0),
('denim', -282.0),
('cute', -321.0),
('striped', -331.0),
('pattern', -337.0)]),
([],
[('chiffon', -89.0),
('summer', -214.0),
('pleated', -250.0),
('sheer', -280.0),
('woven', -286.0),
('crochet', -293.0),
('solid', -295.0),
('cotton', -300.0),
('mesh', -316.0),
('party', -332.0)]),
([],
[('crochet', -188.0),
('chiffon', -204.0),
('summer', -209.0),
('floral', -209.0),
('pattern', -214.0),
('print', -233.0),
('cotton', -252.0),
('sheer', -282.0),
('elegant', -294.0),
('striped', -300.0)]),
(['striped'],
[('striped', 73.0),
('summer', -216.0),
('cotton', -252.0),
('chiffon', -287.0),
('denim', -299.0),
('print', -336.0),
('cute', -357.0),
('linen', -365.0),
('sheer', -372.0),
('chic', -385.0)]),
([],
[('summer', -146.0),
('crochet', -227.0),
('party', -233.0),
('cute', -251.0),
('mesh', -281.0),
('chiffon', -289.0),
('solid', -294.0),
('cotton', -301.0),
('floral', -322.0),
('beach', -323.0)]),
([],
[('cotton', -168.0),
('summer', -197.0),
('print', -211.0),
('striped', -227.0),
('graphic', -248.0),
('denim', -260.0),
('pattern', -302.0),
('floral', -336.0),
('solid', -341.0),
('cute', -341.0)]),
([],
[('print', -165.0),
('denim', -216.0),
('cotton', -260.0),
('striped', -262.0),
('graphic', -301.0),
('leather', -327.0),
('mesh', -339.0),
('stretch', -341.0),
('pattern', -346.0),
('summer', -347.0)]),
([],
[('crochet', -124.0),
('chiffon', -165.0),
('floral', -169.0),
('summer', -170.0),
('sheer', -227.0),
('party', -245.0),
('cute', -260.0),
('mesh', -286.0),
('elegant', -302.0),
('print', -308.0)])]
How can I create a list of lists that contain a list for each tuple in which there are shown just the strings with a number that is higher than -200?如何创建一个列表列表,其中包含每个元组的列表,其中仅显示数字大于 -200 的字符串? The problem comes from the output of an image recognition model and I tried to embed the code of @Andrej Kesely in my for loop on the output of the model in this way:
The problem comes from the output of an image recognition model and I tried to embed the code of @Andrej Kesely in my for loop on the output of the model in this way:
predictions=[]
for x in df_images["Media"]:
p= predict(x, labels, model)
prediction = [i for lst in p for i, v in lst if v > -200]
predictions.append(prediction)
But it gives me this error: "too many values to unpack (expected 2)"但它给了我这个错误:“解包的值太多(预期为 2)”
You can use list comprehension:您可以使用列表理解:
tpl = (
[],
[
("print", -108.0),
("denim", -144.0),
("floral", -158.0),
("summer", -172.0),
("chiffon", -217.0),
("crochet", -272.0),
("cotton", -275.0),
("graphic", -279.0),
("pattern", -281.0),
("sheer", -294.0),
],
)
out = [i for lst in tpl for i, v in lst if v > -200]
print(out)
Prints:印刷:
['print', 'denim', 'floral', 'summer']
EDIT: With the new list predictions
from the question:编辑:使用来自问题的新列表
predictions
:
out = [[i for i, v in lst if v > -200] for _, lst in predictions]
print(out)
Prints:印刷:
[['print', 'summer', 'chiffon'], ['chiffon'], ['crochet'], ['striped'], ['summer'], ['cotton', 'summer'], ['print'], ['crochet', 'chiffon', 'floral', 'summer']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.