<p>您可以通过迭代<code>input_list</code>并检查<em>ith</em>元素是否大于<em>ith+1</em>元素来实现这一点。如果上述条件为真,则将<em>ith</em>元素附加到say<code>temp</code>列表。一旦这个条件失败,将<em>ith+1</em>添加到<code>temp</code>列表为什么,因为例如,如果<code>list=[7,6,5,8]</code>根据我们的逻辑,我们将<code>7</code>添加到<code>temp</code>列表和<code>6</code>。但是当我们在<code>5</code>时,条件就失败了。但是我们还要添加<code>5</code>以获得所需的输出。现在将<code>temp</code>列表附加到<code>descend_lists</code>。你知道吗</p>
<p>代码:</p>
<pre><code>numbers=[7,1,6, 17, 18, 25, 25, 21, 11, 5 ,3 ,3,26,25]
def split1(X:list)->list:
new_list=[] #intermediate list
final=[] #final will contain the answer you wanted
for idx in range(0,len(X)-1):
if(X[idx]>=X[idx+1]):
new_list.append(X[idx])
else:
if new_list !=[]: # you come into else only X[i]< X[i+1]
new_list.append(X[idx]) # that implies x[i] should be in new_list so i added x[idx] again
final.append(new_list) # now add the whole new_list to final
new_list=[] # Now flush the values in new_list
new_list.append(X[-1]) # I ran my for loop len(X)-1 times so the last element needs to be added
final.append(new_list)
return final # The answer you wanted
print(split1(numbers))
</code></pre>
<p>输出:</p>
<pre><code>[[7, 1], [25, 25, 21, 11, 5, 3, 3], [26, 25]]
</code></pre>
<blockquote>
<p>If the list is sorted i.e <code>list=[1,2,3,4]</code> then output would be <code>[[4]]</code> and it makes sense because it's the only element where our condition is true.</p>
</blockquote>
<p>希望这对你有帮助。你知道吗</p>