擅长:python、mysql、java
<p>在感兴趣的分隔符上拆分它,然后在每个切片大小之间循环:</p>
<pre><code>s = 'MayaWindow|toolBar2|MainShelfLayout|formLayout14|ShelfLayout|Custom'
l = s.split('|')
new_list = [l[:idx] for idx in range(1, len(l)+1)]
</code></pre>
<p>结果:</p>
^{pr2}$
<p>或者,如果您想要字符串而不是<code>list</code>s,<code>join()</code>它们回到分隔符上:</p>
^{3}$
<p>结果:</p>
<pre><code>>>> for item in new_list:
... print(item)
...
MayaWindow
MayaWindow|toolBar2
MayaWindow|toolBar2|MainShelfLayout
MayaWindow|toolBar2|MainShelfLayout|formLayout14
MayaWindow|toolBar2|MainShelfLayout|formLayout14|ShelfLayout
MayaWindow|toolBar2|MainShelfLayout|formLayout14|ShelfLayout|Custom
</code></pre>