【问题标题】:Sorting a file in Python在 Python 中对文件进行排序
【发布时间】:2016-02-27 12:59:41
【问题描述】:

我是 Python 新手,正在努力对文件中的数字进行排序。我想做一个冒泡或插入排序,并按升序排列文件中的数字。数字不是整数。这是我目前所拥有的:

input_file=open("C:\\Users\\Rachel\\Documents\\Natural Sciences MSci\\Year 2\\Env Sci\\Comp Modelling\\Pptn data.txt", "r")
header_line = input_file.readline()
for line in input_file:
print line

list=input_file.read()
print list

def insertion_sort(items):
for i in range(1, len(items)):
    j=i
    while j>0 and items[j] < items[j-1]:
        temp=items[j]
        items[j]=items[j-1]
        items[j-1]=temp
        j=j-1

insertion_sort(list)
print 'After sorting:', list

在我运行这个之后,未排序的列表被打印出来并且短语After sorting:appears 但没有数字的排序列表:D

我确定我遗漏了一些明显的东西,但我尝试了很多不同的方法,但似乎无法得到它。

任何帮助都会很棒 谢谢!

【问题讨论】:

  • 另请注意,您可能希望使用 file.readlines() 而不是 file.read() 将文件的行读入列表。
  • 我认为您将不得不拆分 while j&gt;0 and items[j] &lt; items[j-1]:。如果不满足第二个条件,这是很正常的情况,你不会进入循环,所以 j 不会被递减。

标签: python file sorting


【解决方案1】:

一个问题是最初的 for 循环耗尽了输入文件中的数据,因此在随后的input_file.read() 中没有任何内容可读取。 read() 也将返回一个字符串,而不是一个列表。但是在任何情况下,您的插入排序函数都在一个空字符串上运行,所以它什么都不做。

您可以通过在 for 循环之后查找文件的开头来解决第一个问题。第二个问题可以通过使用splitlines()按行拆分输入来解决:

header_line = next(input_file)
for line in input_file:
    print line

input_file.seek(0)
next(input-file)    # skip header again
list=input_file.read().splitlines()
print list

但这样做可能会更好:

with open('input_file') as input_file:
    header_line = next(input_file).strip()
    numbers = [line.strip() for line in input_file]
    # if you really want to print them out first...
    for number in numbers:
        print number

    insertion_sort(numbers)

注意此代码不会将文件中的数据转换为任何数字类型(例如整数),因为您说数字不是整数......那它们是什么?不转换为数字类型意味着您的排序函数将根据数字字符串的 ASCII 排序序列进行排序,因此'10' 将在'2' 之前排序。

如果数字可以是浮点数,则在读取文件时可以这样做:

numbers = [float(line) for line in input_file]

现在您的排序函数会将 1 或 1.0 等数字排序为浮点数。

【讨论】:

    【解决方案2】:

    很抱歉让您混淆了您的目标。这是正确的代码:

    input_file=open("C:\\Users\\Rachel\\Documents\\Natural Sciences MSci\\Year 2\\Env Sci\\Comp Modelling\\Pptn data.txt", "r")
    header_line = input_file.readline()
    
    list=input_file.read().split()
    
    
    def insertion_sort(items):
        for i in range(1, len(items)):
            j = list[i]
            i = i - 1
            while i >= 0:
                if j < list[i]:
                    list[i + 1] = list[i]
                    list[i] = j
                    i = i - 1
                else:
                    break
    
    
    insertion_sort(list)
    print 'After sorting:', list
    

    【讨论】:

      【解决方案3】:

      您的算法似乎运行良好。我在我的电脑上尝试了以下操作。 我创建了一个名为 numbers.txt 的文件并按以下方式放置数字:

      23
      23.4
      4
      5
      6.7
      1
      0
      6
      34
      

      然后运行如下代码:

      def insertion_sort(items):
          for i in range(1, len(items)):
              j = i
              while j > 0 and items[j] < items[j-1]:
                  temp = items[j]
                  items[j] = items[j - 1]
                  items[j - 1] = temp
                  j = j - 1
      
      numbers = open("numbers.txt").read().split()
      numbers = [float(number) for number in numbers]
      print "Before sorting: ", numbers
      insertion_sort(numbers)
      print "After sorting: ", numbers
      

      这给了我以下输出:

      Before sorting:  [23.0, 23.4, 4.0, 5.0, 6.7, 1.0, 0.0, 6.0, 34.0]
      After sorting:  [0.0, 1.0, 4.0, 5.0, 6.0, 6.7, 23.0, 23.4, 34.0]
      

      我希望这会有所帮助。

      【讨论】:

      • 问题不在于排序算法,而在于读取文件的代码。
      • @mhawke 哦,我没有意识到你已经发布了相同的内容。没有刷新页面。对此感到抱歉。
      猜你喜欢
      • 2015-06-14
      • 2020-11-21
      • 2017-10-22
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多