python可以自动拆分表格吗_Python:如何根据特定元素拆分列表

1586010002-jmsa.png

If we have the following list in Python

sentence = ["I", "am", "good", ".", "I", "like", "you", ".", "we", "are", "not", "friends", "."]

How do I split this to get a list which contains elements that finish with the full stop? So i want to get the following elements in my new list:

["I","am","good","."]

["I","like","you","."]

["we","are","not","friends","."]

My attempts so far:

cleaned_sentence = []

a = 0

while a < len(sentence):

current_word = sentence[a]

if current_word == "." and len(cleaned_sentence) == 0:

cleaned_sentence.append(sentence[0:sentence.index(".")+1])

a += 1

elif current_word == "." and len(cleaned_sentence) > 0:

sub_list = sentence[sentence.index(".")+1:-1]

sub_list.append(sentence[-1])

cleaned_sentence.append(sub_list[0:sentence.index(".")+1])

a += 1

else:

a += 1

for each in cleaned_sentence:

print(each)

Running this on sentence produces

['I', 'am', 'good', '.']

['I', 'like', 'you', '.']

['I', 'like', 'you', '.']

解决方案

You can use itertools.groupby:

from itertools import groupby

i = (list(g) for _, g in groupby(sentence, key='.'.__ne__))

print([a + b for a, b in zip(i, i)])

This outputs:

[['I', 'am', 'good', '.'], ['I', 'like', 'you', '.'], ['we', 'are', 'not', 'friends', '.']]

If your list doesn't always end with '.' then you can use itertools.zip_longest instead:

sentence = ["I", "am", "good", ".", "I", "like", "you", ".", "we", "are", "not", "friends"]

i = (list(g) for _, g in groupby(sentence, key='.'.__ne__))

print([a + b for a, b in zip_longest(i, i, fillvalue=[])])

This outputs:

[['I', 'am', 'good', '.'], ['I', 'like', 'you', '.'], ['we', 'are', 'not', 'friends']]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值