擅长:python、mysql、java
<p>当你需要定义列表长度和不需要的时候,我想解释一些事情</p>
<blockquote>
<p>First, you don't need to define list length at the beginning in
general case:</p>
</blockquote>
<p>您可以简单地执行以下操作:</p>
<pre><code>#Just for example
new_list=[]
for i in map(chr,range(97,101)):
new_list.append(i)
print(new_list)
</code></pre>
<p>输出:</p>
<pre><code>['a', 'b', 'c', 'd']
</code></pre>
<blockquote>
<p>Yes you need to define a empty list when you have another list for
index items like this:</p>
</blockquote>
<pre><code>matching_words=[None]*10
index_list=[4,3,1,2]
for i in zip(list(map(chr,range(97,101))),index_list):
matching_words.insert(i[1],i[0])
print(matching_words)
</code></pre>
<p>输出:</p>
<pre><code>[None, 'c', 'd', None, None, 'b', None, 'a', None, None, None, None, None, None]
['c', 'd', 'b', 'a']
</code></pre>
<p>在这个程序中,我们必须按索引列表显示整数的顺序插入chracter,所以如果我们不在之前定义list,那么正如你所看到的,我们在索引4处插入第一个chr,而索引4不在那里</p>
<p>在您的情况下,如果您有第二种情况,请尝试此操作以删除其余情况:</p>
<pre><code>print([i for i in matching_words if i!='none'])
</code></pre>
<p>否则,如果你是第一个病例</p>