简体   繁体   English

当行/列值在数据库中更改时,如何在for循环中使用break-continue?

[英]How to use break-continue inside for-loop when row/column values changes inside the database?

In the given data set I need to multiply values from different blocks with each other. 在给定的数据集中,我需要将不同块中的值彼此相乘。

I want to inject break-continue within for loop but the examples I have looked so far isn't quite helping. 我想在for循环中插入break-continue,但是到目前为止我看到的示例并没有帮助。 Actually, this data is just a part of the big data so I need some explanation on how - why break-continue works. 实际上,这些数据只是大数据的一部分,因此我需要一些解释-为什么中断连续起作用。

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

My code is as follows: 我的代码如下:

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)

The result is: 结果是:

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

But, in the print I want 但是,我要打印

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

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

So, how to inject the break and continue function within this for-loop? 那么,如何在这个for循环中注入break和Continue函数呢?

  • I don't want to use a fixed value for blocks since this is a long file and the block value will change. 我不想为块使用固定值,因为这是一个长文件,并且块值将更改。

  • Also, the row with same block values may not be next to each other. 同样,具有相同块值的行可能不会彼此相邻。

  • Also, i don't need solution on pandas since this is just a part of big file which is exclusive mined using for-if-else loop. 另外,我不需要大熊猫的解决方案,因为这只是大文件的一部分,大文件是使用for-if-else循环独家开发的。

Thanks much in advance ! 在此先感谢!

This is way easier using the groupby() function, otherwise if you're sure you won't have too many blocks, use a dictionary. 使用groupby()函数会更容易,否则,如果您确定不会有太多的块,请使用字典。

If this is to solve a programming exercise and you're guaranteed to have blocks in a sequence, keep a partial sum of the current block and if you encounter a new block, print the sum and then reset it to 0. 如果这是为了解决编程问题,并且确保您有顺序排列的块,请保留当前块的部分总和;如果遇到新的块,请打印总和,然后将其重置为0。

You dont need break nor continue , all you have to do is: 您不需要break也不continue ,您要做的就是:

#1 keep track of current block #1跟踪当前块

#2 collect data for the current block #2收集当前块的数据

#3 when the block change, process current collected data #3当块更改时,处理当前收集的数据

#4 after the loop end, repeat point #3 #4循环结束后,重复点#3

It's a quite common pattern FWIW. 这是一种很常见的FWIW模式。

Example: 例:

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

which yields: 产生:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM