当前位置: 首页 > 知识库问答 >
问题:

Python通过将字符串拆分为列表并分隔元素来计算字符串的长度

贾骏
2023-03-14

我的问题涉及在文本中查找包含分号的句子,并查找分号前后的单词数。我知道如何用分号分割所有内容,但是我得到了两个字符串,但我似乎无法计算字符串中的单词?

文本看起来像:

"What does Bessie say I have done?" I asked.
"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."  
A breakfast-room adjoined the drawing-room, I slipped in there.
It contained a bookcase: I soon possessed myself of a volume, taking care that it should be one stored with pictures.
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.
Folds of scarlet drapery shut in my view to the right hand; to the left were the clear panes of glass, protecting, but not separating me from the drear November day.
At intervals, while turning over the leaves of my book, I studied the aspect of that winter afternoon.
Afar, it offered a pale blank of mist and cloud; near a scene of wet lawn and storm-beat shrub, with ceaseless rain sweeping away wildly before a long and lamentable blast.
I returned to my book--Bewick's History of British Birds: the letterpress thereof I cared little for, generally speaking; and yet there were certain introductory pages that, child as I was, I could not pass quite as a blank.
They were those which treat of the haunts of sea-fowl; of "the solitary rocks and promontories" by them only inhabited of the coast of Norway, studded with isles from its southern extremity, the Lindeness, or Naze, to the North Cape--     "Where the Northern Ocean, in vast whirls,    
Boils round the naked, melancholy isles
Of farthest Thule; and the Atlantic surge
Pours in among the stormy Hebrides."
Nor could I pass unnoticed the suggestion of the bleak shores of Lapland, Siberia, Spitzbergen, Nova Zembla, Iceland, Greenland, with "the vast sweep of the Arctic Zone, and those forlorn regions of dreary space,--that reservoir of frost and snow, where firm fields of ice, the accumulation of centuries of winters, glazed in Alpine heights above heights, surround the pole, and concentre the multiplied rigours of extreme cold."  
Of these death-white realms I formed an idea of my own: shadowy, like all the half-comprehended notions that float dim through children's brains, but strangely impressive.

到目前为止,我取得了以下成就:

count = -1
for line in open("jane_eyre_sentences.txt"):
  words = line.strip("\n")
  count += 1
  if ";" in words:
    wordssplit = words.split(";")


    print("Line " +str(count)+ ":", wordssplit )

我使用计数的原因是每次迭代后计数增加1,因此句子被标记。我已经去掉了句子末尾的段落,如果句子中包含分号,我也用分号将它们分开。

到目前为止,我只试着打印单词split,看看它能给我带来什么。

Line 1: ['"Jane, I don\'t like cavillers or questioners', ' besides, there is something truly forbidding in a child taking up her elders in that manner.']
Line 2: ['Be seated somewhere', ' and until you can speak pleasantly, remain silent."  ']
Line 5: ['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.']
Line 6: ['Folds of scarlet drapery shut in my view to the right hand', ' to the left were the clear panes of glass, protecting, but not separating me from the drear November day.']
Line 8: ['Afar, it offered a pale blank of mist and cloud', ' near a scene of wet lawn and storm-beat shrub, with ceaseless rain sweeping away wildly before a long and lamentable blast.']
Line 9: ["I returned to my book--Bewick's History of British Birds: the letterpress thereof I cared little for, generally speaking", ' and yet there were certain introductory pages that, child as I was, I could not pass quite as a blank.']
Line 10: ['They were those which treat of the haunts of sea-fowl', ' of "the solitary rocks and promontories" by them only inhabited of the coast of Norway, studded with isles from its southern extremity, the Lindeness, or Naze, to the North Cape--     "Where the Northern Ocean, in vast whirls,    ']
Line 12: ['Of farthest Thule', ' and the Atlantic surge']

共有1个答案

贾实
2023-03-14

我不知道你的目的是什么,但据我所知,你只是想知道一个文件中每一行的单词数在半表的左边和右边?对的如果是这样的话,这应该对你有用。

with open(textfile,'rt',encoding='utf-8')as infile:
    for line in infile:
        for i,e in enumerate(line.split(' ')):
            if e.endswith(';'):
                print("--> {}\nContains {} words to the left, and {} words to the right\n".format(line.strip(),i+1,len(line.strip().split(" "))-(i+1)))
 类似资料:
  • 问题内容: 有没有办法在不知道字符串长度的情况下,将一个字符长的字符串切成4个字符串,每个字符长? 例如: 问题答案:

  • 问题内容: 我需要在SQL Server 2012中将一列中的字符串拆分为一个字符,并将每个字符串拆分成它自己的列。 例如:如果我有一个栏,我需要把它拆分成,,,,,与每个这些转化为自己列。 要拆分的列的长度可能会有所不同,因此我需要使其尽可能地动态。 问题答案: 您可以这样做: 输出: 这是动态版本:

  • 问题内容: 我有一个字符串说: 如何在php中将其分为2个变量,分别为数字元素和字母元素? number元素的长度可以是1到4 say之间的任何长度,字母元素可以填充其余部分,使每个order_num总共10个字符。 我已经找到了php 函数…但是在我的情况下不知道如何制作它,因为数字的数量在1到4之间,并且之后的字母是随机的,因此无法拆分成一个特定的字母。请尽可能提供具体帮助! 问题答案: 您可

  • 问题内容: 我有一个值为的字符串。我想将字符串分成两个字符串,值为的字符串和的值为字符串。 正确的功能/语法是什么? 我已经看过了,但是找不到将数据返回到两个单独的字符串中的实际语法。 问题答案: 该功能适用于:

  • 问题内容: 一些外部数据供应商希望给我一个数据字段-管道分隔的字符串值,我觉得这很难处理。 没有应用程序编程语言的帮助,有没有办法将字符串值转换为行? 但是,存在一个困难,该字段具有未知数量的定界元素。 有问题的数据库引擎是MySQL。 例如: 问题答案: 它可能没有我最初想象的那么困难。 这是一种通用方法: 计算分隔符的出现次数 循环多次,每次获取一个新的定界值并将该值插入第二个表中。

  • 问题内容: 我希望我的Python函数拆分一个句子(输入)并将每个单词存储在列表中。我当前的代码拆分了句子,但没有将单词存储为列表。我怎么做? 问题答案: 这应该足以将每个单词存储在列表中。 words已经是句子中单词的列表,因此不需要循环。 其次,这可能是拼写错误,但是你的循环有些混乱。如果你确实确实想使用附加,它将是: 不