gpt4 book ai didi

python - python中的排序算法帮助

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:58 24 4
gpt4 key购买 nike

我一直在研究一个程序,该程序将从两个文件中获取信息,然后将这些信息按排序顺序写到一个文件中。

所以我所做的是将文件的每一行存储为列表中的一个元素。我创建了另一个函数,将每个元素拆分为一个二维数组,我可以在其中轻松访问名称变量。从那里我想创建一个嵌套的 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()

我在尝试将最大值附加到排序列表、从拆分列表中删除该值并重新运行代码时遇到了问题。

最佳答案

根据 Joran 的建议,使用 sorted 方法要容易得多,而且已经内置了。我已经编辑了您的阅读方法,以便它构建两个元组列表,代表行和行的长度。 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)

编辑:我在元组中添加了标记,以便您可以跟踪哪些行来自哪个文件。没有它们可能会有点困惑

关于python - python中的排序算法帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51069629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com