1

我正在尝试在 Python 中编写一个脚本来删除两列之间的制表符/空格(一列带有 x 坐标,另一列带有 y 坐标)加上用逗号分隔的列,并列出每列的最大值和最小值(2 个值对于每个 x 和 y 坐标),最后是这样的:

10000000    6000000
20000000    6100000
30000000    6200000
40000000    6300000
50000000    6400000

看起来像:

10000000,6000000
20000000,6100000
30000000,6200000
40000000,6300000
50000000,6400000

10000000 50000000 60000000 640000000

我是新手,非常感谢任何帮助。非常感谢!

4

2 回答 2

1

您可以使用该csv模块进行输出;只需循环输入文件并用于str.split()将行拆分为行:

import csv

minimum = [float('inf'), float('inf')]
maximum = [float('-inf'), float('-inf')]

with open(inputfilename, 'r') as infile:
    with open(outputfilename, 'wb') as outfile:
        writer = csv.writer(outfile)
        for line in infile:
            row = map(int, line.split())
            minimum = map(min, zip(minimum, row))
            maximum = map(max, zip(maximum, row))
            writer.writerow(row)

x_extremes, y_extremes = zip(minimum, maximum)
print ' '.join(map(str, x_extremes)), ' '.join(map(str, y_extremes))

和starter 值使以后更容易计算最小和最大坐标float('inf')float('-inf')

最后一行是

于 2013-09-07T13:21:46.970 回答
0

这会起作用:

li = []
for line in lines:
    li.append(', '.join(line.split('\t')))

first_column = [int(x.split(',')[0]) for x in li]
second_column = [int(x.split(' ')[1]) for x in li]

for x in li:
    print (x)
print(min(first_column), max(first_column), min(second_column), max(second_column))
于 2013-09-07T13:09:58.930 回答