擅长:python、mysql、java
<p>你说得对!在Python中,当您将一个列表作为参数传递给函数,或者将一个列表赋给另一个变量时,实际上是在传递一个指向它的指针。你知道吗</p>
<p>这是为了提高效率;如果每次执行上述操作时都单独复制一份1000项列表,那么程序将消耗太多的内存和时间。你知道吗</p>
<p>为了在Python中克服这个问题,可以使用<code>= originalList[:]</code>或<code>= list(originalList)</code>复制一维列表:</p>
<pre><code>sentence = sentences[0][:] # or sentence = list(sentences[0])
sentence.insert(0,startc)
sentence.append(endc)
print(sentence) # modified
print(sentences[0]) # not modified
</code></pre>
<p>如果需要复制2D列表,可以考虑使用<a href="http://docs.python.org/3.3/tutorial/datastructures.html#list-comprehensions" rel="nofollow" title="list comprehension">list comprehension</a>。你知道吗</p>