我知道数值在python中是不可变的.我还读过python中的所有东西都是对象.我只是想知道数字类型是否也是python中的对象.因为如果它们是对象,那么变量实际上是引用变量对吧?这是否意味着如果我将一个数字传递给一个函数并在函数内修改它,那么会创建两个带有两个引用的数字对象?python中是否存在原始数据类型的概念?
注意:我也把它当作对象.但是在python导师中可视化说不同:http://www.pythontutor.com/visualize.html#mode=edit
def test(a):
a+=10
b=100
test(b)
Run Code Online (Sandbox Code Playgroud)
或者它是可视化工具中的缺陷?
import numpy as np
W = np.array([0,1,2])
W1 = W
W1 += np.array([2,3,4])
print W
W = np.array([0,1,2])
W1 = W
W1 = W1 + np.array([2,3,4])
print W
Run Code Online (Sandbox Code Playgroud)
上面的代码会发生变异W,但较低的代码不会发生变异W.为什么?
有人可以解释一下这种行为:
>>> a = {'hello':'world' , 'good':'food'}
>>> b = [1,2]
>>> b = b + a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "dict") to list
>>> b += a
>>> b
[1, 2, 'good', 'hello'] <--- Why do the keys get added when dict cannot be added to a list
>>>
Run Code Online (Sandbox Code Playgroud) 在Python(2.7)中发现了前所未有的有趣之处.
这个:
a = []
a += "a"
Run Code Online (Sandbox Code Playgroud)
工作和结果是:
>>> a
>>> ["a"]
Run Code Online (Sandbox Code Playgroud)
但
a = []
a = a + "a"
Run Code Online (Sandbox Code Playgroud)
给
>>> TypeError: can only concatenate list (not "str") to list
Run Code Online (Sandbox Code Playgroud)
有人可以解释原因吗?谢谢你的回答.
我需要一个很好的解释(参考)来解释(for)循环中的NumPy切片.我有三个案子.
def example1(array):
for row in array:
row = row + 1
return array
def example2(array):
for row in array:
row += 1
return array
def example3(array):
for row in array:
row[:] = row + 1
return array
Run Code Online (Sandbox Code Playgroud)
一个简单的案例:
ex1 = np.arange(9).reshape(3, 3)
ex2 = ex1.copy()
ex3 = ex1.copy()
Run Code Online (Sandbox Code Playgroud)
收益:
>>> example1(ex1)
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> example2(ex2)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> example3(ex3)
array([[1, 2, 3],
[4, 5, 6], …Run Code Online (Sandbox Code Playgroud) 我注意到了一个(看似)奇怪的分配行为,这让我多次犯了编程错误.
首先参见以下示例:
>>> i = 0
>>> t = (i,)
>>> t
(0,)
>>> i += 1
>>> t
(0,)
Run Code Online (Sandbox Code Playgroud)
正如所料,t即使在增加值之后,唯一元素的值也不会改变i.
现在看到以下内容:
>>> l = [0]
>>> t = (l,)
>>> t
([0],)
>>> l[0] += 1
>>> t
([1],) # <- ?
Run Code Online (Sandbox Code Playgroud)
我不明白为什么最初的零t现在是一个; 如果我通过引用增加它t...
>>> t[0][0] += 1
Run Code Online (Sandbox Code Playgroud)
...我知道它的值已经改变了,但在前面的例子中并非如此,其中只有l在递增时才显式引用.
我有两个问题:
在这种情况下,为什么会x += y产生不同的结果x = x + y呢?
import numpy as np
x = np.repeat([1], 10)
y = np.random.random(len(x))
x += y
print x
# Output: [1 1 1 1 1 1 1 1 1 1]
x = x + y
print x
# Output: [ 1.50859536 1.31434732 1.15147365 1.76979431 1.64727364
# 1.02372535 1.39335253 1.71878847 1.48823703 1.99458116]
Run Code Online (Sandbox Code Playgroud) 我虽然这a += b只是一个捷径a = a + b.看起来并不完全.这是一个例子:
>>> a = [1, 2, 3]
>>> b = a
>>> b += [4, 5, 6]
>>> b
[1, 2, 3, 4, 5, 6]
>>> a # is also changed
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
但这可以按预期工作:
>>> a = [1, 2, 3]
>>> b = a
>>> b = b + [4, 5, 6]
>>> b
[1, 2, 3, 4, 5, 6]
>>> a # not changed
[1, …Run Code Online (Sandbox Code Playgroud) 如果我有以下列表:
a = [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我运行以下代码:
for x in a:
x += 1
Run Code Online (Sandbox Code Playgroud)
看来这不会改变列表a.
但是,如果我执行以下操作:
for i in range(0, len(a)):
a[i] += 1
Run Code Online (Sandbox Code Playgroud)
这将修改'a'的内容.
所以我猜x并且以不同的方式a[i]指代元素a.究竟是什么导致了这种差异?他们每个人都指的是a什么?
我正在浏览python的dis包.我试过代码来看看它是如何工作的
>>> def get():
... x=4
... y=x+3 ............ this line
... z=8
... return y
Run Code Online (Sandbox Code Playgroud)
然后运行dis.dis(get)打印上述代码的字节码指令.
对于x+3字节码指令来说BINARY_ADD,当通过包文档时,我找到了另一个术语INPLACE_ADD
虽然在那里他们已经提到了什么,INPLACE_ADD但我没有得到区别.
我有两个问题
一个).BINARY_ADD和之间的区别是什么INPLACE_ADD
B).如何编写代码以便字节码指令显示INPLACE_ADD
python ×10
numpy ×3
arrays ×1
iterator ×1
list ×1
loops ×1
object ×1
python-2.7 ×1
python-3.x ×1