Simple question.
What hashing algorithm is used in python's default dict?
>>> g = { 'a' : 1, 'b' : 2, 'c' : 3 }
>>> g
{'a': 1, 'c': 3, 'b': 2}
>>> g.keys()
['a', 'c', 'b']
>>>
I expected ['a','b','c'] on g.keys()
Linear probe (guess not)? double hash?
There is no guarantee that Python will use any particular method - different implementations could use any one they wish. dicts are unordered, so it doesn't matter how it's implemented (provided it fulfills certain obligations).
As to how CPython does it...
Related
All the keys of my dict are strings, I don't want to implement the dict.get() function but with the binary search or any log(n) algorithm, is there any other way to do that job? Imagine a sorted dictionary.
Just a small example:
dic = {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'aux' : 5}
dic.get('a')
I am looking for a way to find all keys of a dictionary that are mapped to a given value (similar to what np.where would do for arrays). For instance :
dict = {'a':12, 'b':0, 'c':0}
print(where(dict, 0))
>>> ['b', 'c']
Obviously, I can do it with :
[key for key in dict.keys() if dict[key]==0]
but I would like to know if there is already a Python function that performs this operation.
Thanks a lot :)
Basically there is no python built in function for performing it.
You can use filter:
my_dict = {
'a': 12,
'b': 0,
'c': 0
}
print(list(filter(lambda x: my_dict[x] == 0, my_dict)))
You can use also numpy:
keys = np.array(list(my_dict.keys()))
values = np.array(list(my_dict.values()))
print(keys[values == 0])
I'd like to be able to setup a mock that allows me to return something when I apply the builtin dict method.
I've tried using __iter__ to no avail. I can't seem to get anything but an empty dictionary:
import mock
mocked_object = mock.MagicMock()
mocked_object.__iter__.return_value = [1, 2, 3]
dict(mocked_object)
# {}
From dict documentation
If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an iterable object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value.
MagicMock objects expose a keys method just because are mock objects and so dict() will consider they mapping objects. Unfortunately that way is little bit complicated to use if we want that on dict call mock become a dictionary with predefined key-values. The follow examples show how to implement a dict conversion to a predefined dictionary by use mapping object protocol:
>>> m = MagicMock()
>>> d = {"a":"A", "b":"B", "c":"C"}
>>> m.keys.return_value.__iter__.return_value = ["a", "b", "c"]
>>> m.__getitem__.side_effect = ["A","B","C"]
>>> dict(m)
{'a': 'A', 'c': 'C', 'b': 'B'}
>>> #Little bit generic
>>> m.keys.return_value.__iter__.return_value = d.keys()
>>> m.__getitem__.side_effect = lambda k:d[k]
>>> dict(m)
{'a': 'A', 'c': 'C', 'b': 'B'}
Both are little bit hard to read and in our test we would like something of simpler to read.
To lead dict to use iterator instead mapping we can just remove keys method from our mock and set __iter__.return_value:
>>> del m.keys
>>> m.__iter__.return_value = [("a","A"),("b","B"),("c","C")]
>>> dict(m)
{'a': 'A', 'c': 'C', 'b': 'B'}
>>> #Little bit generic
>>> m.__iter__.return_value = d.items()
>>> dict(m)
{'a': 'A', 'c': 'C', 'b': 'B'}
IMHO that is a simple and neat way to set up your mock and get a predefined dictionary from dict call.
Actually I think you need to do something like this:
mocked_object.keys.return_value.__iter__.return_value = [1, 2, 3]
With this, the dict method will give you an object with those keys, and the result of getattr(mocked_object, '1') (so, another mocked method) as values. I think you might be able to do what you want by mocking the keys() method as well if you want more control on the result.
I have a dictionary
d = {'a':1, 'b':2, 'c':3}
I need to remove a key, say c and return the dictionary without that key in one function call
{'a':1, 'b':2}
d.pop('c') will return the key value - 3 - instead of the dictionary.
I am going to need one function solution if it exists, as this will go into comprehensions
How about this:
{i:d[i] for i in d if i!='c'}
It's called Dictionary Comprehensions and it's available since Python 2.7.
or if you are using Python older than 2.7:
dict((i,d[i]) for i in d if i!='c')
Why not roll your own? This will likely be faster than creating a new one using dictionary comprehensions:
def without(d, key):
new_d = d.copy()
new_d.pop(key)
return new_d
If you need an expression that does this (so you can use it in a lambda or comprehension) then you can use this little hack trick: create a tuple with the dictionary and the popped element, and then get the original item back out of the tuple:
(foo, foo.pop(x))[0]
For example:
ds = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
[(d, d.pop('c'))[0] for d in ds]
assert ds == [{'a': 1, 'b': 2}, {'a': 4, 'b': 5}]
Note that this actually modifies the original dictionary, so despite being a comprehension, it's not purely functional.
When you invoke pop the original dictionary is modified in place.
You can return that one from your function.
>>> a = {'foo': 1, 'bar': 2}
>>> a.pop('foo')
1
>>> a
{'bar': 2}
solution from me
item = dict({"A": 1, "B": 3, "C": 4})
print(item)
{'A': 1, 'B': 3, 'C': 4}
new_dict = (lambda d: d.pop('C') and d)(item)
print(new_dict)
{'A': 1, 'B': 3}
this will work,
(lambda dict_,key_:dict_.pop(key_,True) and dict_)({1:1},1)
EDIT
this will drop the key if exist in the dictionary and will return the dictionary without the key,value pair
in python there are functions that alter an object in place, and returns a value instead of the altered object, {}.pop function is an example.
we can use a lambda function as in the example, or more generic below
(lambda func:obj:(func(obj) and False) or obj)
to alter this behavior, and get a the expected behavior.
I have the key of a python dictionary and I want to get the corresponding index in the dictionary. Suppose I have the following dictionary,
d = { 'a': 10, 'b': 20, 'c': 30}
Is there a combination of python functions so that I can get the index value of 1, given the key value 'b'?
d.??('b')
I know it can be achieved with a loop or lambda (with a loop embedded). Just thought there should be a more straightforward way.
Use OrderedDicts: http://docs.python.org/2/library/collections.html#collections.OrderedDict
>>> x = OrderedDict((("a", "1"), ("c", '3'), ("b", "2")))
>>> x["d"] = 4
>>> x.keys().index("d")
3
>>> x.keys().index("c")
1
For those using Python 3
>>> list(x.keys()).index("c")
1
Dictionaries in python (<3.6) have no order. You could use a list of tuples as your data structure instead.
d = { 'a': 10, 'b': 20, 'c': 30}
newd = [('a',10), ('b',20), ('c',30)]
Then this code could be used to find the locations of keys with a specific value
locations = [i for i, t in enumerate(newd) if t[0]=='b']
>>> [1]
You can simply send the dictionary to list and then you can select the index of the item you are looking for.
DictTest = {
'4000':{},
'4001':{},
'4002':{},
'4003':{},
'5000':{},
}
print(list(DictTest).index('4000'))
No, there is no straightforward way because Python dictionaries do not have a set ordering.
From the documentation:
Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.
In other words, the 'index' of b depends entirely on what was inserted into and deleted from the mapping before:
>>> map={}
>>> map['b']=1
>>> map
{'b': 1}
>>> map['a']=1
>>> map
{'a': 1, 'b': 1}
>>> map['c']=1
>>> map
{'a': 1, 'c': 1, 'b': 1}
As of Python 2.7, you could use the collections.OrderedDict() type instead, if insertion order is important to your application.
#Creating dictionary
animals = {"Cat" : "Pat", "Dog" : "Pat", "Tiger" : "Wild"}
#Convert dictionary to list (array)
keys = list(animals)
#Printing 1st dictionary key by index
print(keys[0])
#Done :)