dictview

Python 3.0 - dict methods return views - why?

寵の児 提交于 2019-12-21 03:42:48
问题 dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. http://docs.python.org/dev/3.0/whatsnew//3.0.html First of all how is a view different from an iterator? Secondly, what is the benefit of this change? Is it just for performance reasons? It doesn't seem intuitive to me, i.e., I'm asking for a list of thing (give me all your keys) and I'm getting something else back. Will this confuse people? 回答1: You are effectively getting a list. It's just not a copy

dict.keys()[0] on Python 3

有些话、适合烂在心里 提交于 2019-12-13 01:06:40
问题 I have this sentence: def Ciudad(prob): numero = random.random() ciudad = prob.keys()[0] for i in prob.keys(): if(numero > prob[i]): if(prob[i] > prob[ciudad]): ciudad = i else: if(prob[i] > prob[ciudad]): ciudad = i return ciudad But when I call it this error pops: TypeError: 'dict_keys' object does not support indexing is it a version problem? I'm using Python 3.3.2 回答1: dict.keys() is a dictionary view. Just use list() directly on the dictionary instead if you need a list of keys, item 0

Python: Understanding dictionary view objects

白昼怎懂夜的黑 提交于 2019-12-09 20:57:57
问题 I've been trying to understand built-in view objects return by .items() , .values() , .keys() in Python 3 or similarly by .viewitems() , .viewvalues() , .viewkeys() . There are other threads on that subject but none (even the doc) seems to described how they work internally. The main gain here seems to be efficienty compared to the copy of type list returned in Python 2. There are often compared to a window to the dictionnary items (like in this thread). But what is that window and why is it

Python: Understanding dictionary view objects

谁说我不能喝 提交于 2019-12-04 14:41:39
I've been trying to understand built-in view objects return by .items() , .values() , .keys() in Python 3 or similarly by .viewitems() , .viewvalues() , .viewkeys() . There are other threads on that subject but none (even the doc ) seems to described how they work internally. The main gain here seems to be efficienty compared to the copy of type list returned in Python 2. There are often compared to a window to the dictionnary items (like in this thread ). But what is that window and why is it more efficient ? The only thing I can see is that the view objects seems to be set-like objects,

Python 3.0 - dict methods return views - why?

我的梦境 提交于 2019-12-04 00:55:40
dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. http://docs.python.org/dev/3.0/whatsnew//3.0.html First of all how is a view different from an iterator? Secondly, what is the benefit of this change? Is it just for performance reasons? It doesn't seem intuitive to me, i.e., I'm asking for a list of thing (give me all your keys) and I'm getting something else back. Will this confuse people? You are effectively getting a list. It's just not a copy of the internal list, but something that acts as if it where a list but only represents the internal state.

Why are set methods like .intersection() not supported on set-like objects?

喜欢而已 提交于 2019-12-02 07:06:13
问题 In Python 3.7, I'd like to calculate the intersection of two dictionaries' keys. To do this, I'd like to call the .intersection() method on their keys() , however it does not work. .keys() produces a set-like object, however most set methods don't work on it. What works however is the extremely unknown bitwise operator overloads for set-like objects, like & . m = {'a':1, 'b':2} n = {'b':3, 'c':4} m.keys().intersection(n.keys()) # Pythonic, but doesn't work m.keys() & n.keys() # works but not

Why are set methods like .intersection() not supported on set-like objects?

拈花ヽ惹草 提交于 2019-12-02 06:38:14
In Python 3.7, I'd like to calculate the intersection of two dictionaries' keys. To do this, I'd like to call the .intersection() method on their keys() , however it does not work. .keys() produces a set-like object, however most set methods don't work on it. What works however is the extremely unknown bitwise operator overloads for set-like objects, like & . m = {'a':1, 'b':2} n = {'b':3, 'c':4} m.keys().intersection(n.keys()) # Pythonic, but doesn't work m.keys() & n.keys() # works but not readable set(m.keys()).intersection(set(n.keys())) # works, readable, but too verbose I find that the &

Inconsistent behaviour between dict.items and dict.values

岁酱吖の 提交于 2019-12-02 05:07:17
问题 Note: code examples in python3, but the question stands for python2 as well (replacing .keys with .viewkeys , etc) dict objects provide view methods which (sometimes) support set operations: >>> {'a': 0, 'b': 1}.keys() & {'a'} {'a'} >>> {'a': 0, 'b': 1}.items() & {('a', 0)} {('a', 0)} But the values view does not support set operators: >>> {'a': 0, 'b': 1}.values() & {0} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for &: 'dict

Inconsistent behaviour between dict.items and dict.values

做~自己de王妃 提交于 2019-12-02 02:32:06
Note: code examples in python3, but the question stands for python2 as well (replacing .keys with .viewkeys , etc) dict objects provide view methods which (sometimes) support set operations: >>> {'a': 0, 'b': 1}.keys() & {'a'} {'a'} >>> {'a': 0, 'b': 1}.items() & {('a', 0)} {('a', 0)} But the values view does not support set operators: >>> {'a': 0, 'b': 1}.values() & {0} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for &: 'dict_values' and 'set' I understand that a dict value can be a non-hashable object, so it is not always

dict.keys()[0] on Python 3

回眸只為那壹抹淺笑 提交于 2019-11-28 08:02:31
I have this sentence: def Ciudad(prob): numero = random.random() ciudad = prob.keys()[0] for i in prob.keys(): if(numero > prob[i]): if(prob[i] > prob[ciudad]): ciudad = i else: if(prob[i] > prob[ciudad]): ciudad = i return ciudad But when I call it this error pops: TypeError: 'dict_keys' object does not support indexing is it a version problem? I'm using Python 3.3.2 dict.keys() is a dictionary view. Just use list() directly on the dictionary instead if you need a list of keys, item 0 will be the first key in the (arbitrary) dictionary order: list(prob)[0] or better still just use: next(iter