正如@sudo_O 所说,对于这项任务,有比 python 更有效的工具。但是,这是一个可能的解决方案:
from itertools import imap, repeat
import csv
fun = pow
with open('m.in', 'r') as input_file :
with open('m.out', 'wb') as out_file:
inpt = csv.reader(input_file, delimiter=' ')
out = csv.writer(out_file, delimiter=' ')
for row in inpt:
row = [ int(e) for e in row] #conversion
opt = repeat(2, len(row) ) # square power for every value
# write ( function(data, argument) )
out.writerow( [ str(elem )for elem in imap(fun, row , opt ) ] )
在这里,它将每个数字自己相乘,但您可以通过更改 opt 将其配置为仅乘以第二列:opt = [ 1 + (col == 1) for col in range(len(row)) ]
(2 代表 col 1,1 否则)