我有一个列表:
.checkbox-lg .custom-control-label::before,
.checkbox-lg .custom-control-label::after {
width: 1.55rem;
height: 1.55rem;
}
.checkbox-lg .custom-control-label {
padding-left: 6px;
}
input[type='checkbox'] {
display: block;
}
我想把这个列表分成存款子列表和每月子列表。我只需要根据 \n (新行)拆分它。喜欢:
<div class="custom-control custom-checkbox checkbox-lg">
<input type="checkbox" class="custom-control-input" id="delivery-checkbox-18" name="deliveryAddresses[]" value="">
<label class="custom-control-label" for="delivery-checkbox-18">fsdfs<br>
sdfsd <br>
sdfsdf <br>
434 sdfsd
</label>
</div>
但我不知道如何修复它。
答案 0 :(得分:2)
你可以使用两次列表理解,
prices=['deposit:2000$ \n monthly:500$','deposit:3500$ \n monthly:800$','deposit:2800$ \n monthly:670$']
deposit = [item.split("\n")[0].strip() for item in prices]
monthly = [item.split("\n")[1].strip() for item in prices]
您也可以使用 zip 一步完成此操作,
deposits, monthly = zip(*[item.split("\n ") for item in prices])
请注意,退回的存款和每月将是元组。如果需要,您可以将其投射到列表中。
答案 1 :(得分:1)
类似于下面的内容
prices = ['deposit:2000$ \n monthly:500$', 'deposit:3500$ \n monthly:800$', 'deposit:2800$ \n monthly:670$']
deposit = []
monthly = []
for p in prices:
d, m = p.split('\n')
deposit.append(d)
monthly.append(m)
print(f'{monthly} {deposit}')
输出
[' monthly:500$', ' monthly:800$', ' monthly:670$'] ['deposit:2000$ ', 'deposit:3500$ ', 'deposit:2800$ ']