Python-不确定如何在变量名中使用计数器?

Python-不确定如何在变量名中使用计数器?,python,dictionary,counter,Python,Dictionary,Counter,我对编码一无所知。这让我很难受,我觉得我错过了一些超基本的东西,但我就是无法解决 非常简单,我使用for循环查看文本文件的每一行。 如果发现空行,我希望更新计数器 然后,我希望使用计数器信息来更改变量名,然后将文本文件中的特定行保存到其中 因此,在脚本末尾,变量名paragraphxlinx将与文本文件中的相应段落相对应 但我似乎无法理解如何使用计数器信息来生成变量 PARA_COUNT = 1 LINE_COUNT = 1 for x in CaptionFile_data.splitlin

我对编码一无所知。这让我很难受,我觉得我错过了一些超基本的东西,但我就是无法解决

非常简单,我使用for循环查看文本文件的每一行。 如果发现空行,我希望更新计数器

然后,我希望使用计数器信息来更改变量名,然后将文本文件中的特定行保存到其中

因此,在脚本末尾,变量名paragraphxlinx将与文本文件中的相应段落相对应

但我似乎无法理解如何使用计数器信息来生成变量

PARA_COUNT = 1
LINE_COUNT = 1

for x in CaptionFile_data.splitlines():
    if x != "": #still in existing paragraph
        (PARA_COUNT_LINE_COUNT) = x #I know this syntax isn't right just not sure what to put here?
        LINE_COUNT += 1

    else: #new paragraph has started
        PARA_COUNT += 1
        LINE_COUNT = 1

动态创建变量是不好的做法。您应该使用或列表,例如:

paragraphs= {1: ['line1','line2'],
            2: ['line3','line4']}

如果坚持使用变量,可以使用:


动态创建变量是不好的做法。您应该使用或列表,例如:

paragraphs= {1: ['line1','line2'],
            2: ['line3','line4']}

如果坚持使用变量,可以使用:


在代码中,可以使用列表存储空行:

PARA_COUNT = 0
LINE_COUNT = 0
emptyLines = []
for x in CaptionFile_data.splitlines():
    if x != "": #still in existing paragraph
        emptyLines.append(x)        
        LINE_COUNT += 1
    else: #new paragraph has started
        PARA_COUNT += 1
--试一试--

第一行查找空行的所有索引
空行索引列表的长度是数据中的空行数

emptyLineIndex = [i for i,line in enumerate( CaptionFile_data.splitlines()) if not line]
numberOfEmpty = len( emptyLineIndex  )

在代码中,可以使用列表存储空行:

PARA_COUNT = 0
LINE_COUNT = 0
emptyLines = []
for x in CaptionFile_data.splitlines():
    if x != "": #still in existing paragraph
        emptyLines.append(x)        
        LINE_COUNT += 1
    else: #new paragraph has started
        PARA_COUNT += 1
--试一试--

第一行查找空行的所有索引
空行索引列表的长度是数据中的空行数

emptyLineIndex = [i for i,line in enumerate( CaptionFile_data.splitlines()) if not line]
numberOfEmpty = len( emptyLineIndex  )