May-30-2020, 09:50 AM
(This post was last modified: May-30-2020, 10:28 AM by DreamingInsanity.)
There's probably many ways of doing this, but personally I would use the filter() method:
This could be compressed into 1 line if you wanted:
ages = {"0.25": "a", "0.1":"b", "-0.03": "c", "-0.25": "d"} def myFunc(x): if (not -0.1 <= float(x) <= 0.1): return x ages = { k:ages[str(k)] for k in filter(myFunc, ages) }Ages would then become:
{'0.25': 'a', '-0.25': 'd'}
This could be compressed into 1 line if you wanted:
ages = { k:ages[str(k)] for k in filter(lambda x: not -0.1 <= float(x) <= 0.1, ages) }EDIT: I read your post wrong. I didn't realise the floats were the the values and not the keys. Instead you can do this:
ages = {"1": "0.25", "2": "0.1", "3": "-0.03", "4": "-0.25"} ages = { k:ages[str(k)] for k in filter(lambda x: not -0.1 <= float(ages[x]) <= 0.1, ages) } print(ages)which gets you:
Output:{'1': '0.25', '4': '-0.25'}