【问题标题】:How to use break-continue inside for-loop when row/column values changes inside the database?当数据库中的行/列值发生变化时,如何在for循环中使用break-continue?
【发布时间】:2016-11-28 15:53:56
【问题描述】:

在给定的数据集中,我需要将来自不同块的值相乘。

我想在 for 循环中注入 break-continue,但到目前为止我看到的示例并没有多大帮助。实际上,这些数据只是大数据的一部分,所以我需要解释一下为什么 break-continue 起作用。

So,for X(set) I have to multiply: 0.25*0.1*0.83 (since they belong to same block

block   X_set
2480    0.25
2480    0.1
2480    0.083
2651    0.43
2651    0.11
2651    0.23

我的代码如下:

test_q = open("test_question.txt", "r+")
header = test_q.readline()
data_q = test_q.read().rstrip("\n")

product=1.0
value_list = []

row = data_q.split("\n")

for line in row:
    block = line.split("\t")
    X_list = block[1].split()
    X_value = float(block[1])
    value_list = value_list + X_list
    product = product*X_value

print(value_list)
print(product)

结果是:

['0.25', '0.1', '0.083', '0.43', '0.11', '0.23']
2.2573925000000003e-05

但是,在我想要的印刷品中

['0.25', '0.1', '0.083']
0.0002075

['0.43', '0.11', '0.23']
0.010879

那么,如何在这个 for 循环中注入 break 和 continue 函数?

  • 我不想为块使用固定值,因为这是一个长文件,并且块值会改变。

  • 另外,具有相同块值的行可能不相邻。

  • 另外,我不需要处理 pandas,因为这只是使用 for-if-else 循环独家挖掘的大文件的一部分。

非常感谢!

【问题讨论】:

    标签: python if-statement for-loop break continue


    【解决方案1】:

    你不需要break也不需要continue,你所要做的就是:

    #1 跟踪当前区块

    #2 收集当前区块的数据

    #3 当块发生变化时,处理当前采集的数据

    #4循环结束后,重复第3点

    这是一个非常常见的 FWIW 模式。

    例子:

    import csv
    import operator
    
    # for test
    from StringIO import StringIO
    
    dat = """
    block\tX_set
    2480\t0.25
    2480\t0.1
    2480\t0.083
    2651\t0.43
    2651\t0.11
    2651\t0.23
    """
    f = StringIO(dat.strip())
    f.seek(0)
    
    reader = csv.reader(f, delimiter="\t")
    header = reader.next()
    current = None
    values = []
    results = []
    
    for key, val in reader:
        # is this a new block ?
        if key != current:
            # ok, do we have values ?
            if values:
                # let's compute our product for collected values
                #print block
                #print reduce(operator.mul, map(float, blockvals))
                # and collect them for later use
                results.append((values, reduce(operator.mul, map(float, values))))
            # reset the values collector for the block
            values = []
            # and the current block so we can detect next change
            current = key
    
        # in all cases we want to collect data for this block
        values.append(val)
    
    # handle the last block
    if values:
        results.append((values, reduce(operator.mul, map(float, values))))
    
    
    # and now display our results:
    for blockvals, product in results:
        print blockvals
        print product
    

    产生:

    ['0.25', '0.1', '0.083']
    0.002075
    ['0.43', '0.11', '0.23']
    0.010879
    

    【讨论】:

    • 谢谢@burno。但是,看起来 StringIO 在我正在使用的 python 3.6 中发生了变化。另外,尝试稍微修改代码但不成功,直到没有。另一个问题是大多数数据挖掘已经使用 for-if-else 完成。我将不得不找到一种方法来适应这种情况。同时,您能否在我尝试使用的解决方案中输入一些内容。见下文。
    • 您不需要 StringIO 部分,它只是作为示例的一部分,以避免依赖外部文件。 csv 模块的使用同上 - 这就是您应该用来解析 csv(或在您的情况下为 tsv)文件的方法,但您仍然可以进行手动解析。有用的部分是使用current 跟踪“块更改”并使用values 收集当前块的值并在块更改时处理它们(以及循环后的最后一次)。这是您必须在现有代码中改造的部分。
    • HI bruno:这种循环对我来说很新鲜,我刚刚开始使用来自生物学背景的编程。您能否在每个步骤中添加更多详细信息。另外,我看到 reduce 没有定义,我也收到了“reduce is not defined”之类的错误消息。
    • 我刚刚意识到 reduce 函数在 python 3 中已更改为 functools。我更改了它但仍然出现错误:results.append((values, functools(operator.mul, map(float, values))))TypeError: 'module' object is not callable
    • functools 是模块,而不是函数。你想要functools.reduce()
    【解决方案2】:

    使用groupby() 函数更容易,否则如果您确定不会有太多块,请使用字典。

    如果这是为了解决编程练习,并且保证您有顺序的块,请保留当前块的部分总和,如果遇到新块,请打印总和然后将其重置为 0。

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 2017-02-02
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多