alubhate
alubhate

Reputation: 25

python dictionary keys are string and values are a list of floats; how to filter with cutoff?

I am using python.

I have a dictionary; where keys are "ASN A 74" (bold face) and the values are list of floats. I would like to check if each float value of a list <= 50.00 and it should return me keys of the corresponding values if it passes the above condition.

Here are a few lines of sample data:

ASN A 74 [60.88, 57.21, 54.43, 52.94, 58.27, 59.37, 61.42, 60.15]

LYS A 72 [76.41, 74.61, 73.91, 73.82, 74.27, 74.17, 72.36, 70.58, 70.12]

TYR A 75 [48.85, 45.25, 42.33, 41.89, 46.64, 47.71, 47.23, 46.54, 46.32, 46.61, 45.16, 46.0]

ASP A 73 [72.02, 70.8, 66.95, 65.35, 73.96, 75.88, 76.19, 77.57]

THANKS IN ADVANCE!!

Upvotes: 0

Views: 205

Answers (2)

Epsi95
Epsi95

Reputation: 9047

all returns True if all of the elements in an iterable are true.

d = {
    "ASN A 74" : [60.88, 57.21, 54.43, 52.94, 58.27, 59.37, 61.42, 60.15],

    "LYS A 72" : [76.41, 74.61, 73.91, 73.82, 74.27, 74.17, 72.36, 70.58, 70.12],

    "TYR A 75" : [48.85, 45.25, 42.33, 41.89, 46.64, 47.71, 47.23, 46.54, 46.32, 46.61, 45.16, 46.0],

    "ASP A 73" :[72.02, 70.8, 66.95, 65.35, 73.96, 75.88, 76.19, 77.57]
}

result = [k for k,v in d.items() if all(map(lambda x: x<=50.0, v))]

print(result) #['TYR A 75']

Upvotes: 2

U13-Forward
U13-Forward

Reputation: 71610

If it's a dictionary:

for k, v in dct.items():
    if all(i <= 50 for i in v):
        print(k)

Upvotes: 1

Related Questions