擅长:python、mysql、java
<p>您可以在这里使用<code>itertools.groupby()</code>+<code>zip()</code>:</p>
<pre><code>from itertools import groupby
test = [
"Vlan101",
"1.1.1.0/24",
"Vlan111",
"2.1.1.0/24",
"Vlan701",
"3.1.1.0/24",
"Vlan703",
"4.1.1.0/24",
"5.5.5.0/24",
]
print(
[
(x,) + y
for x, y in zip(
(x_ for x_ in test if x_.startswith("Vlan")),
(
tuple(g)
for k, g in groupby(test, key=lambda x__: x__.startswith("Vlan"))
if not k
),
)
]
)
# [('Vlan101', '1.1.1.0/24'), ('Vlan111', '2.1.1.0/24'), ('Vlan701', '3.1.1.0/24'), ('Vlan703', '4.1.1.0/24', '5.5.5.0/24')]
</code></pre>