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

Python 拆分列表中的元素和单独的字符串,然后计算长度,python,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-

如果我有几行代码

"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'对象没有属性“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(";")]
编辑:包含来自的更多信息


请解释:
我将如何实际分隔此列表。
,如何分隔此列表?什么?@depperm在make it like[“简,我不喜欢开玩笑的人或发问的人][“此外,一个孩子以那种方式对待她的长辈是非常禁止的。”]?好了,伙计们,我练习的问题是“你们的任务是找到包含分号的句子,并找出分号前后的单词数。”。使用jane_eyre_Sequences.txt,其中包含第一章的摘录和完整的标点符号。您的程序应该打印出行号,然后是分号前后的字数,用分号分隔。“那么”坐在某个地方;在你能愉快地说话之前,保持沉默。“程序应该打印:第2行:3;8到目前为止,我已经得到:count=-1,用于open中的行(“jane_-eyre_-Sequences.txt”):words=Line.strip(“\n”)count+=1 if”;“在words:wordssplit=words.split(;”)print(“Line”+str(count)+“:”,wordssplit)好的,假设您有一个普通的代码行,就像这样,那么您将如何拆分它???只有一行代码行1:['”简,我不喜欢吹毛求疵的人或发问的人,“此外,一个孩子以那种方式对待她的长辈确实是一件令人生畏的事。”
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)