3

I have a dict like this tmp_dic = {'0.0': 'val0', '1': 'val1', 'key3': 'val2'} . Now during parsing a file, I wanna check if for instance a float value of lets say 1.0 is in tmp_dic keys or not? I have a simple logic like this, but seems it can return wrong answer sometimes.

str(int(1.0)) in tmp_dic.keys()

Do I need to check if numeric string is integer or float before checking if they exists in the keys? Thanks for the hints.

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
Alex Man
  • 457
  • 4
  • 19
  • That depends on whether you want floats and integer string representation to be considered as equal? – yatu Mar 24 '20 at 16:24
  • What exactly do you mean by "wrong answer sometimes"? P.S. instead of using `in tmp_dic.keys()` just use `in tmp_dic`, it does the same thing but is much more efficient. – Mark Ransom Mar 24 '20 at 18:16

1 Answers1

1

you can use:

def gen_float(l):
    for f in l:
        try:
            yield float(f)
        except ValueError:
            pass
any(v == 1.00 for v in gen_float(tmp_dic.keys()))

output:

True

is better to cast your keys to float than converting your check float to an int than to a string, for ex. if your check float is 1.23 this will be converted to 1


or you can use:

1.0 in gen_float(tmp_dic.keys())

as @HeapOverflow was suggesting


it will be better if at tmp_dict creation time you will try to convert the keys into floats, than the search for a float in your dict will be O(1) time complexity


if you do not have strings like '1.0000' you can also use:

str(1.0) in tmp_dict or str(int(1.0)) in tmp_dict
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Doing a linear search of a dictionary kind of defeats the purpose of using a dictionary in the first place. – Mark Ransom Mar 24 '20 at 16:52
  • @MarkRansom the OP may have '1.00', or '1.00000' in which case a linear approach is a good approach – kederrac Mar 24 '20 at 16:54
  • No, linear is not a good approach, it's a slow one. If that's what you want to do, it would be more productive to have a `list` of key,value pairs. The better approach is to make the keys be a consistent format so that equivalent keys always match. – Mark Ransom Mar 24 '20 at 18:08
  • P.S. using floats as dictionary keys isn't a good idea either, see https://stackoverflow.com/q/588004/5987 – Mark Ransom Mar 24 '20 at 18:11
  • I m pretty sure the OP hasn't this corner case, is more about 1 decimal, as I can see, but the OP may clarify – kederrac Mar 24 '20 at 18:13
  • @kederrac- Thanks a lot. That works. And you are right. I have only 1 decimal. – Alex Man Mar 24 '20 at 18:47
  • @MarkRansom- Thanks for the comment. I agrrte with your point about using float in the dict. The float in the above example dict comes from output of pd.cut for binning a numerical value which I convert bins to a dict. Not sure if that is the best idea though. – Alex Man Mar 24 '20 at 18:49
  • what about using a decimal.Decimal? – JL Peyret Mar 25 '20 at 00:19