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] >>&…
比如我们有个列表: number = [']; 如果我们需要将列表里的元素转换为数字呢?最常用的大家可能会想到使用列表推导式: number = ['] number = [int(x) for x in number] print(number); 输出:[1, 2, 3, 4] 还有一种技巧,更方便: number = ['] number = list(map(int, number)) # 使用内置map返回一个map对象,再用list将其转换为列表 print(number) 输出:[…