Reputation: 59
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:
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:
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? 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)"
Upvotes: 0
Views: 64
Reputation: 195593
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:
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']]
Upvotes: 1