基于Indice Python附加列表

时间:2018-04-22 14:44:40

标签: python

如果我有如下文字:

There was no possibility of taking a walk that day
We had been wandering indeed in the leafless shrubbery an hour in the morning but since dinner (Mrs Reed when there was no company dined early) the cold winter wind had brought with it clouds so sombre and a rain so penetrating that further out door exercise was now out of the question
I was glad of it I never liked long walks especially on chilly afternoons dreadful to me was the coming home in the raw twilight with nipped fingers and toes and a heart saddened by the chidings of Bessie the nurse and humbled by the consciousness of my physical inferiority to Eliza John and Georgiana Reed
The said Eliza John and Georgiana were now clustered round their mama in the drawing room she lay reclined on a sofa by the fireside and with her darlings about her (for the time neither quarrelling nor crying) looked perfectly happy

我想创建一个列表,例如:(我想在句子前面附上一个列表,但取决于句子的位置)

0: There was no possibility of taking a walk that day
1: We had been wandering indeed in the leafless shrubbery an hour in the morning but since dinner (Mrs Reed when there was no company dined early) the cold winter wind had brought with it clouds so sombre and a rain so penetrating that further out door exercise was now out of the question
2: I was glad of it I never liked long walks especially on chilly afternoons dreadful to me was the coming home in the raw twilight with nipped fingers and toes and a heart saddened by the chidings of Bessie the nurse and humbled by the consciousness of my physical inferiority to Eliza John and Georgiana Reed
3: The said Eliza John and Georgiana were now clustered round their mama in the drawing room she lay reclined on a sofa by the fireside and with her darlings about her (for the time neither quarrelling nor crying) looked perfectly happy

我该怎么办?

显然,第一句话是0,因为当我们考虑在indice规则中计算时,本段的第一句将被视为0。我试过的是

for line in open("MYEXAMPLETEXT"):
  line_strip = line.rstrip()
  words = line_strip.split()

  print(line_strip[0],":", words)

除了显然line_strip [0]错误,因为它打印出每个单词中的第一个字母,所以它看起来像:

T: There was no possibility of taking a walk that day
W: We had been wandering indeed in the leafless shrubbery an hour in the morning but since dinner (Mrs Reed when there was no company dined early) the cold winter wind had brought with it clouds so sombre and a rain so penetrating that further out door exercise was now out of the question
I: I was glad of it I never liked long walks especially on chilly afternoons dreadful to me was the coming home in the raw twilight with nipped fingers and toes and a heart saddened by the chidings of Bessie the nurse and humbled by the consciousness of my physical inferiority to Eliza John and Georgiana Reed
T: The said Eliza John and Georgiana were now clustered round their mama in the drawing room she lay reclined on a sofa by the fireside and with her darlings about her (for the time neither quarrelling nor crying) looked perfectly happy

我只需要根据文本的整个段落来考虑句子的位置???

1 个答案:

答案 0 :(得分:0)

没关系。我意识到我可以先初始化一个值,例如

  count = -1
  for line in open("MYEXAMPLETEXT"):
   line_strip = line.rstrip()
   words = line_strip.split()
  count += 1

  print(count,":", words)