gpt4 book ai didi

python - 拆分列表中的元素并分隔字符串,然后计算长度

转载 作者:行者123 更新时间:2023-12-01 09:29:52 25 4
gpt4 key购买 nike

如果我有几行代码,例如

"Jane, I don't like cavillers or questioners; besides, there is something truly forbidding in a child taking up her elders in that manner.
Be seated somewhere; and until you can speak pleasantly, remain silent."
I mounted into the window- seat: gathering up my feet, I sat cross-legged, like a Turk; and, having drawn the red moreen curtain nearly close, I was shrined in double retirement.

我想用“;”分割每行的“字符串”或句子标点符号,我会做

for line in open("jane_eyre_sentences.txt"):
words = line.strip("\n")
words_split = words.split(";")

但是,现在我会得到这样的文本字符串,

["Jane, I don't like cavillers or questioners', 'besides, there is something truly forbidding in a child taking up her elders in that manner.']
[Be seated somewhere', 'and until you can speak pleasantly, remain silent."']
['I mounted into the window- seat: gathering up my feet, I sat cross-legged, like a Turk', 'and, having drawn the red moreen curtain nearly close, I was shrined in double retirement.']

现在它已经在此列表中创建了两个单独的元素。

我实际上如何分离这个列表。

我知道我需要一个“for”循环,因为它需要处理所有行。我需要使用另一个“split”方法,但是我尝试过“\n”以及“,”,但它不会生成答案,并且 python 的内容显示“AttributeError: 'list' object has no attribute 'split ’”。这意味着什么?

一旦我分成单独的字符串,我想计算每个字符串的长度,所以我会执行 len() 等。

最佳答案

您可以像这样迭代创建的单词列表:

for line in open("jane_eyre_sentences.txt"):
words = line.strip("\n")
for sentence_part in words.split(";"):
print(sentence_part) # will print the elements of the list
print(len(sentence_part) # will print the length of the sentence parts

或者,如果您只需要每个部分的长度:

for line in open("jane_eyre_sentences.txt"):
words = line.strip("\n")
sentence_part_lengths = [len(sentence_part) for sentence_part in words.split(";")]

编辑:更多信息来自your second post .

for count, line in enumerate(open("jane_eyre_sentences.txt")):
words = line.strip("\n")
if ";" in words:
wordssplit = words.split(";")
number_of_words_per_split = [(x, len(x.split())) for x in wordsplit]
print("Line {}: ".format(count), number_of_words_per_split)

关于python - 拆分列表中的元素并分隔字符串,然后计算长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50063078/

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