回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>有人能解释为什么在test1中,nums是<em>[[0,0],-1,-1],[0,0],[0,0]]</em>,而不是test2吗?据我所知,xxx中的python for xx非常类似于任何其他语言中的for循环,并采用元素对元素的方式。那么在for循环中使用unpack和not有什么不同呢?谢谢</p>
<pre><code>test([[0,0],[0,0],[0,0],[0,0]])
test2([[0,0],[0,0],[0,0],[0,0]])
def test1(self, nums):
ctn = 0
for e in nums:
ctn += 1
u, v = e
if ctn == 2:
e[0] = e[1] = -1
print(nums) #[[0,0],[-1,-1],[0,0],[0,0]]
def test2(self, nums):
ctn = 0
for u, v in nums:
ctn += 1
if ctn == 2:
u = v = -1
print(nums) #[[0,0],[0,0],[0,0],[0,0]]
</code></pre>