gpt4 book ai didi

python - 如何停止 for 循环并使其进一步运行?

转载 作者:行者123 更新时间:2023-12-01 05:13:24 24 4
gpt4 key购买 nike

我是Python新手。我希望你能帮助我;)

我有一个数据:

[('Senators', 'NNS'), ('and', 'CC'), ('a', 'DT'), ('good', 'JJ'), ('teacher', 'NN'),
('believes', 'VBZ'), ('in', 'IN'), ('the', 'DT'), ('possibilities', 'NNS'), ('of',
'IN'),
('every', 'DT'), ('boy', 'NN'), ('and', 'CC'), ('girl', 'NN'), ('.', '.'),
('The','DT'),
('good', 'JJ'), ('teacher', 'NN'), ('sees', 'VBZ'), ('what', 'WP')......]

我想在列表中创建一个列表,其中包含每个句子,使其看起来像:

[ [('Senators', 'NNS'), ('and', 'CC'), ('a', 'DT'), ('good', 'JJ'), ('teacher', 'NN'),
('believes', 'VBZ'), ('in', 'IN'), ('the', 'DT'), ('possibilities', 'NNS'), ('of',
'IN'),
('every', 'DT'), ('boy', 'NN'), ('and', 'CC'), ('girl', 'NN')], [('The', 'DT'),
('good', 'JJ'), ('teacher', 'NN'), ('sees', 'VBZ'), ('what', 'WP')] ...]

但我不知道该怎么做:(我已经在 for 循环的帮助下尝试过

  for el in data:
if el[0] != ('.' or '?' or '!'): # finds only points((
sentences.append(el)

当他找到一个点时我怎样才能停止循环?我怎样才能让它看得更远并写入新列表?

最佳答案

sentences = []
sentence = []

for word, code in data:
sentence.append((word, code))
if word in '.?!':
sentences.append(sentence)
sentence = []

如果您不想将要点包含在句子中:

sentences = []
sentence = []

for word, code in data:
if word in '.?!':
sentences.append(sentence)
sentence = []
else:
sentence.append((word, code))

关于python - 如何停止 for 循环并使其进一步运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23709663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com