Posts: 21
Threads: 14
Joined: Dec 2019
May-30-2020, 09:24 AM
(This post was last modified: May-30-2020, 09:24 AM by donnertrud.)
Hi guys,
I got a dictionary with values ranging from 0.25 to -0.25. I am looking for a way, to automatically drop all keys that are either
0 < value < 0.1
or
0 > value > -0.1
so basically all keys' values between 0.1 and -0.1 should be dropped from the dict. Is there an easy way to do that?
Posts: 60
Threads: 9
Joined: May 2020
You could iterate over the dictionary, checking each key value and if your conditions are met append that value to a new dictionary.
I don't think you can just drop key values from an existing dictionary.
Posts: 21
Threads: 14
Joined: Dec 2019
That would work aswell! Can you recall a code to do that ?
Posts: 161
Threads: 36
Joined: Jun 2018
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:
1 2 3 4 5 6 7 |
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:
1 |
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:
1 2 3 4 |
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'}
Posts: 45
Threads: 13
Joined: Feb 2020
May-30-2020, 09:55 AM
(This post was last modified: May-30-2020, 09:55 AM by WiPi.)
Here's another straightforward method
1 2 3 4 5 6 7 |
dictOfVals = { 1 : 0.2 , 2 : - 0.1 , 3 : 2 , 4 : 0.1 , 5 : 0.05 }
newDict = dict ()
for value in dictOfVals.values():
if value > 0.1 or value < - 0.1 :
print (value)
|
Posts: 21
Threads: 14
Joined: Dec 2019
(May-30-2020, 09:55 AM)WiPi Wrote: Here's another straightforward method
1 2 3 4 5 6 7 |
dictOfVals = { 1 : 0.2 , 2 : - 0.1 , 3 : 2 , 4 : 0.1 , 5 : 0.05 }
newDict = dict ()
for value in dictOfVals.values():
if value > 0.1 or value < - 0.1 :
print (value)
|
How would I also get the corresponding key for each value?
Posts: 2,168
Threads: 35
Joined: Sep 2016
A dictionary has a pop method:
https://docs.python.org/3/library/stdtyp...p#dict.pop Wrote:pop(key[, default])
If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.
Posts: 21
Threads: 14
Joined: Dec 2019
May-30-2020, 10:53 AM
(This post was last modified: May-30-2020, 10:53 AM by donnertrud.)
Thanks, with your help I made it work!
Now, is there a convenient way to select these keys in a data frame ? E.g. if the dict gave me the keys : "a" , "b" and "e", I want to select them from the the data frame where they are initially coming from :
1 |
df = df.columns[ "a" , "b" , "e" ]
|
I could do it manually, however I got too many returned keys and I am asking out of curiosity.
Posts: 2,128
Threads: 11
Joined: May 2017
Try following with pandas:
1 2 |
df = pd.DataFrame(columns = list ( "abcdefgh" ))
df[[ "a" , "b" , "c" ]]
|
In the previous question you asked how to get also the keys.
If you want to get also the keys, why not return two dicts?
One dict could hold all ok_values and the other can contain the not_ok_values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def classify(mapping):
ok_mapping = {}
nok_mapping = {}
for key, value in mapping.items():
if 0 < value < 0.1 :
ok_mapping[key] = value
else :
nok_mapping[key] = value
return ok_mapping, nok_mapping
dictOfVals = { 1 : 0.2 , 2 : - 0.1 , 3 : 2 , 4 : 0.1 , 5 : 0.05 }
print (classify(dictOfVals))
|
Output: ({5: 0.05}, {1: 0.2, 2: -0.1, 3: 2, 4: 0.1})
|