<p>我希望能够根据列表中的数字插入数字+1、+2,就在该数字的旁边</p>
<p>例如:</p>
<pre><code># existing list
list1 = [1,10,100]
#wanted output:
[1,2,3, 10,11,12, 100,101,102]
#I spaced out the output list for ease of reading/understanding what it is I want to do.
</code></pre>
<p>我也希望能够加起来+7和更多,所以如果可能的话,请以这种方式帮助提高效率</p>
<p>您可以使用简单的嵌套理解:</p>
<pre><code>list1 = [1, 10, 100]
n = 3 # or 7 or whatever
list2 = [x for y in list1 for x in range(y, y+n)]
# [1, 2, 3, 10, 11, 12, 100, 101, 102]
</code></pre>