Re: [Tutor] Behavior of dictionary in mapping keys that evaluate equal
On Wed, May 11, 2016 at 02:00:47PM +0100, khalil zakaria Zemmoura wrote: > Hi, > > Suppose we have a dict > Dic = { True: 'yes', 1: 'No'} > > According to the Python 3 documentation, the keys must have a unique value > so True is converted to integer because of the type coercion (boolean are > subtype of integer) so boolean are winded to integer to be compared. No, not quite. Keys aren't converted to anything. The above is equivalent to: Dic = {} Dic[True] = 'yes' Dic[1] = 'no' That part is straight-forward. But remember that True == 1: py> True == 1 True and they have the same hash: py> hash(True), hash(1) (1, 1) so as far as Python is concerned, they are the same key. The same applies to the float 1.0: py> d = {True: 'yes'} py> d[1] 'yes' py> d[1.0] 'yes' One last fact to remember: if the key is already found in the dict, it isn't replaced. So: py> d = {1: 'no'} py> d[True] = 'yes' py> d {1: 'yes'} Does this now explain why {True: 'yes', 1: 'no'} returns {True: 'no'}? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Behavior of dictionary in mapping keys that evaluate equal
On 11May2016 14:00, khalil zakaria Zemmourawrote: Suppose we have a dict Dic = { True: 'yes', 1: 'No'} According to the Python 3 documentation, the keys must have a unique value so True is converted to integer because of the type coercion (boolean are subtype of integer) so boolean are winded to integer to be compared. As it happens. The core lesson here is that usually you want all your dictionary keys to be the same type. If that's what happen, why when running Dic.items() it return [(True, 'No')] instead of [(1, 'No')] Because the dictionary is assembled sequentially (probably an implementation detail, though since Python expressions are evaluated left to right it might possibly be guarrenteed). So: First 'yes' is loaded into the dict with the key True. The 'No' is loaded, using key 1. This finds the same slot as True, and puts 'No' there. Without changing the key value. So true now has 'No' stored with it. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Behavior of dictionary in mapping keys that evaluate equal
Hi, Suppose we have a dict Dic = { True: 'yes', 1: 'No'} According to the Python 3 documentation, the keys must have a unique value so True is converted to integer because of the type coercion (boolean are subtype of integer) so boolean are winded to integer to be compared. Am I missing something? If that's what happen, why when running Dic.items() it return [(True, 'No')] instead of [(1, 'No')] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] postgreSQL + psycopg2
On Tue, May 10, 2016 at 10:37 PM, nitin chandrawrote: > Thank you Danny, Alan, > > @ Danny > > I added 'str(formt(line1[0]))' will this do ? Unfortunately, I don't know: what makes me hesitant is the following: * The above is doing something just to line1[0], which looks odd. Why just line1[0]? * What's the type of line1? Is it a list of strings? If so, then calling str() on a string is redundant. * I don't know what "formt" is. So, yeah, I have enough questions and uncertainty that I need to hedge. This is not to say that you got it right or wrong, but rather that I don't have enough information to decide either way. There are a lots of little details to get right when we program. Rather than handwave and assume that things are ok, I'll be truthful and admit my ignorance. Hope that makes sense! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor