Python2和Python3中列表推导式的不同 python2 >>> x = 'my girl' >>> lst = [x for x in 'hello'] >>> x 'o' 可以看到x的值被覆盖了 来看python3 python3 >>> x = 'my girl' >>> lst = [x for x in 'hello'] >>> x 'my girl' x的值没有被覆盖,这是因为p…
看下面两个例子: python2的环境:列表中的同名变量名被替换了 >>> x = 'my precious' >>> dummy = [x for x in 'ABC'] >>> dummy ['A', 'B', 'C'] >>> x 'C' python3:列表中的同名变量名没有被替换 >>> x = 'ABC' >>> dummy = [ord(x) for x in x] >>&…