lundi 28 novembre 2016

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. 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 !

Aucun commentaire:

Enregistrer un commentaire