擅长:python、mysql、java
<p>怎么办列表.pop-它将一步到位,并更新列表。在</p>
<pre><code>lines = readlines()
deleted = []
indices_to_delete = random.sample(xrange(len(lines)), 10)
# sort to delete biggest index first
indices_to_delete.sort(reverse=True)
for i in indices_to_delete:
# lines.pop(i) delete item at index i and return the item
# do you need it or its index in the original file than
deleted.append((i, lines.pop(i)))
# write the updated *lines* back to the file or new file ?!
# and you have everything in deleted if you need it again
</code></pre>