如何在f.write()中写入结果

时间:2014-04-13 18:39:56

标签: python fwrite radix-sort

我编写了一个程序,用于基数排序文件中的一些数字。 这是我的代码:

rsort(a):
    if a:
        bins = [ [],[],[],[],[],[],[],[],[],[] ]
        m = max(a)
        r = 1
        while m > r:
            for e in a:
                bins[(e/r)%10].append(e)
            r = r * 10
            a = []
            for i in range(10):
                a.extend(bins[i])
                bins[i] = []
        return a

def readfile(infile):
  grid = []
  f = open(infile, 'r')
  lines = f.readlines()
  f.close()
  return grid

def writefile(outfile):
    grid = []
    f = open(outfile, 'w')
    f.write()
    f.close
    return grid

def main():
    infile = readfile("radix.in")
    outfile = writefile("radix.out")
    sortedvar = rsort(infile)
main()

所以,我尝试在f.write()中插入rsort()和rsort(a),它一直在告诉我错误。如果我把它留空,它告诉我函数需要正好1个参数。我应该怎么做打印基数排序结果文件radix.out?

1 个答案:

答案 0 :(得分:0)

我根本没有查看rsort函数,因为你的问题似乎是关于I / O和函数调用的。这是一种可能性(假设数字都在第一行)。

def readfile(infile):
  nums = []
  f = open(infile, "r")
  line = f.readline()
  f.close()
  return [int(x) for x in line.split()]

def writefile(outfile, nums):
    f = open(outfile, 'w')
    f.write(' '.join([str(x) for x in nums]))
    f.write('\n')
    f.close()

def main():
    nums = readfile("radix.in")
#    nums = rsort(nums)
    writefile("radix.out", nums)

main()