loops - how to add a specific value to a column iteratively in python -


i have line have different columns , 1 particular column (column 2) want add values column (column 12 except 0) iteratively. able first 1 not rest of columns. here example of line

a01     5729384 5730870 bra1000071      117     -       5729384 5730870 255,0,0 4       281,252,145,229 0,380,1030,1257 

and here desired output

a01     5729764 5730870 bra1000071      117     -       5729384 5730870 255,0,0 4       281,252,145,229 0,380,1030,1257 a01     5730794 5730870 bra1000071      117     -       5729384 5730870 255,0,0 4       281,252,145,229 0,380,1030,1257 a01     5732051 5730870 bra1000071      117     -       5729384 5730870 255,0,0 4       281,252,145,229 0,380,1030,1257 

here psuedocode

with open('velvet.test.bed') fh_in:     line in fh_in:         line = line.strip().split()         x1 = line[11].split(',')         print x1         j in x1:             print j             if j!= "0":                 next                 y1 = int(line[1]) + int(j)                 test = line[0], " " + str(y1) + " " + " ".join(line[2:])                 print test 

here output getting...

a01 5729764 5730870 bra1000071 117 - 5729384 5730870 255,0,0 4 281,252,145,229 0,380,1030,1257 a01 5730414 5730870 bra1000071 117 - 5729384 5730870 255,0,0 4 281,252,145,229 0,380,1030,1257 a01 5730641 5730870 bra1000071 117 - 5729384 5730870 255,0,0 4 281,252,145,229 0,380,1030,1257 

here go:

>>> = "a01     5729384 5730870 bra1000071      117     -       5729384 5730870 255,0,0 4       281,252,145,229 0,380,1030,1257" >>> = a.split() >>> add_num = list(map(int,a[-1].split(',')[1:]))   #split whitespace , taken last element, removed 0 , using map convert them int >>> in range(len(add_num)): ...     print(" ".join([b[0]] + [str(int(b[1])+sum(add_num[:i+1]))] + b[2:])) ...  a01 5729764 5730870 bra1000071 117 - 5729384 5730870 255,0,0 4 281,252,145,229 0,380,1030,1257 a01 5730794 5730870 bra1000071 117 - 5729384 5730870 255,0,0 4 281,252,145,229 0,380,1030,1257  a01 5732051 5730870 bra1000071 117 - 5729384 5730870 255,0,0 4 281,252,145,229 0,380,1030,1257 

your code like:

f = open('file') in f:     = a.split()     add_num = list(map(int,a[-1].split(',')[1:]))     in range(len(add_num)):         print(" ".join([b[0]] + [str(int(b[1])+sum(add_num[:i+1]))] + b[2:])) 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -