拆分列表中的元素和单独的字符串,然后计算长度

时间:2018-04-27 13:06:10

标签: 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.

我希望拆分'字符串'或者#34;每一行的句子;"标点符号,我会做的

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'对象没有属性' split'"。这意味着什么?

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

1 个答案:

答案 0 :(得分:1)

您可以遍历创建的单词列表,如下所示:

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)