python中的排序算法帮助

2024-10-01 19:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在玩一个程序,它可以从两个文件中获取信息,然后按顺序将信息写入单个文件。你知道吗

所以我所做的是将文件的每一行作为一个元素存储在一个列表中。我创建了另一个函数,它将每个元素分割成一个二维数组,在这个数组中我可以方便地访问名称变量。从这里我想创建一个嵌套for循环,当它迭代时检查数组中的最高值,从列表中删除值并将其附加到一个新列表,直到有一个排序的列表。你知道吗

我想90%的情况下我是这样的,但是我很难理解排序算法的逻辑。看起来问题越来越复杂,我一直想用指针。如果有人能帮我把这个问题弄清楚,我将不胜感激。你知道吗

import os
from http.cookiejar import DAYS
from macpath import split

# This program reads a given input file and finds its longest line.
class Employee: 
    def __init__(self, EmployeeID, name, wage, days):
        self.EmployeeID = EmployeeID
        self.name = name
        self.wage = wage
        self.days = days

def Extraction(file,file2):
    employList = [] 
    while True:
        line1 = file.readline().strip()
        line2 = file2.readline().strip()
        #print(type(line1))
        employList.append(line1)
        #print(line1)
        employList.append(line2)
        #print(line2)
        if line1 == '' or line2 == '':
            break
    return employList

def Sort(mylist):
    splitlist = []
    sortedlist = []
    print(len(mylist))
    for items in range(len(mylist)):

        #print(mylist[items].split())
        splitlist.append(mylist[items].split())
        print(splitlist)
    #print(splitlist[1][1])
    #print(splitlist[1][2])
    highest = "z"
    print(highest)
    sortingLength = len(splitlist)
    for i in range(10):
        for items in range(len(splitlist)-2):
            if highest > splitlist[items][2]:
                istrue = highest < splitlist[items][2]
                highest = splitlist[items][1]
                print(items)
                print(istrue)
                print('marker')
                print(splitlist[items][2])
            if items == (len(splitlist)-2):
                print("End of list",splitlist[items][2])

        print(highest)
        print(splitlist.index(highest))
    print(splitlist[len(splitlist)-1][2])
    print(sortingLength)

fPath = 'C:/Temp'

fileName = 'payroll1.txt'
fullFileName = os.path.join(fPath,fileName)
fileName2 = 'payroll2.txt'
fullFileName2 = os.path.join(fPath,fileName2)

f = open(fullFileName,'r')
f2 = open(fullFileName2, 'r')

employeeList = Extraction(f,f2)#pulling out each line in the file and placing into a list
Sort(employeeList)

ReportName= "List of Employees:"
marker = '-'* len(ReportName)
print (ReportName + ' \n' + marker)
total = 0

f.close()

有一次,我遇到了一个问题,即有一个最大的值试图将该值附加到sortedlist,从splitlist中删除该值,然后重新运行代码。你知道吗


Tags: 文件inself列表forlenitemsfile
1条回答
网友
1楼 · 发布于 2024-10-01 19:33:41

按照Joran的建议,使用sorted方法更简单,而且已经内置了。我已经编辑了您的reading方法,以便它构建两个元组列表,表示行和行的长度。sorted方法将返回根据键(行长)和降序(reverse=True)排序的列表

from operator import itemgetter

class Employee: 
    def __init__(self, EmployeeID, name, wage, days):
        self.EmployeeID = EmployeeID
        self.name = name
        self.wage = wage
        self.days = days

def Extraction(file,file2):
    employList = [] 
    mylines = [(i, len(l.strip()), 'file1') for i,l in enumerate(file.readlines())]
    mylines2 = [(i, len(l.strip()), 'file2') for i,l in enumerate(file2.readlines())]

    employList = [*mylines, *mylines2]

    return employList

fPath = 'C:/Temp'

fileName = 'payroll1.txt'
fullFileName = os.path.join(fPath,fileName)
fileName2 = 'payroll2.txt'
fullFileName2 = os.path.join(fPath,fileName2)

f = open(fullFileName,'r')
f2 = open(fullFileName2, 'r')

employeeList = Extraction(f,f2)#pulling out each line in the file and placing the line_number and length into a list

f.close()
f2.close()

# Itemgetter will sort on the second element of the tuple, len(line)
# and reverse will put it in descending order
ReportName = sorted(employeeList, key=itemgetter(1), reverse=True)

编辑:我在元组中添加了标记,这样您就可以跟踪哪些行来自哪个文件。如果没有他们可能会有点混乱

相关问题 更多 >

    热门问题