首页 > 解决方案 > 拆分列表中的元素和单独的字符串,然后计算长度

问题描述

如果我有几行代码,这样

"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() 等。

标签: python

解决方案


您可以像这样遍历创建的单词列表:

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)

推荐阅读