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. 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?
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.
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.
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.
You dont need break
nor continue
, all you have to do is:
#1 keep track of current block
#2 collect data for the current block
#3 when the block change, process current collected data
#4 after the loop end, repeat point #3
It's a quite common pattern 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.