python - 拆分列表中的元素和单独的字符串,然后计算长度
问题描述
如果我有几行代码,这样
"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”循环,因为它需要处理所有行。我将需要使用另一种“拆分”方法,但是我已经尝试过“\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(";")]
编辑:从您的第二篇文章中获得更多信息。
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)
推荐阅读
- java - Android 应用程序未运行。模拟器说它一直在关闭
- javascript - 使用 DynamoDB 的 LocalSecondaryIndexes 出现“键架构太大”错误?
- javascript - 如何在外部 JS 中写出今天的特定格式
- php - 当你给一个数组赋值而索引为空时会发生什么?
- javascript - express-validator isAfter 总是假的
- c# - 如何将安装程序单选按钮对话框中的输入用作文件系统 DefaultLocation 的一部分的变量
- python - 有没有办法错开或躲避多个 matplotlib.plot 误差线?
- java - Android File.listFiles() 返回 null
- python - 我只能在python中将某些变量设置为true时记录吗?
- c++ - 我的代码无法将输入验证为整数