当 row/column 值在数据库内发生变化时,如何在 for 循环内使用 break-continue?
How to use break-continue inside for-loop when row/column values changes inside the database?
在给定的数据集中,我需要将来自不同块的值相互相乘。
我想在 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 循环独家挖掘的。
提前致谢!
使用 groupby()
函数更容易,否则如果您确定不会有太多块,请使用字典。
如果这是为了解决编程练习,并且保证序列中有块,请保留当前块的部分和,如果遇到新块,打印总和,然后将其重置为 0 .
你不需要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
在给定的数据集中,我需要将来自不同块的值相互相乘。
我想在 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 循环独家挖掘的。
提前致谢!
使用 groupby()
函数更容易,否则如果您确定不会有太多块,请使用字典。
如果这是为了解决编程练习,并且保证序列中有块,请保留当前块的部分和,如果遇到新块,打印总和,然后将其重置为 0 .
你不需要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