Python 打印已排序的列表无法完全工作,如何解决此问题?
我编写的代码:Python 打印已排序的列表无法完全工作,如何解决此问题?,python,python-3.x,Python,Python 3.x,我编写的代码: opener = open("gymclub.txt", "r") reader = opener.readline() listObjects = [] listNames = [] listPressups = [] listPullups = [] sortedListPressups = [] while reader!="": splitting=reader.split(",")
opener = open("gymclub.txt", "r")
reader = opener.readline()
listObjects = []
listNames = []
listPressups = []
listPullups = []
sortedListPressups = []
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = splitting[1]
pull_ups = splitting[2]
reader = opener.readline()
listPressups.append(press_ups)
sortedListPressups = sorted(listPressups, reverse = True)
print(sortedListPressups)
结果:
['8', '75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10']
预期结果:
['75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10','8']
我做错了什么
编辑:这是文本文件:
你在排序字符串,而不是数字 >>> '8' > '75' 符合事实的 如果要根据这些元素的整数值对其进行排序,可以在排序时传递键参数。这将保持原始数组不变 >>>sortedlistPressups,key=int,reverse=True ['75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10', '8']
您需要将读取的行从字符串转换为整数。现在已排序按字典顺序对列表中的字符串进行排序 opener=opengymclub.txt,r reader=opener.readline listObjects=[] listNames=[] listPressups=[] listPullups=[] sortedListPressups=[] 而读者!=: spliting=reader.split, 名称=正在拆分[0] 按_ups=拆分[1] 上拉=拆分[2] reader=opener.readline listPressups.appendintpress\u ups listPressups.sortreverse=True 打印列表 我已经为你从循环中取出来了。不需要每次迭代都对整个列表进行排序。 我还使用list.sort而不是sorted对列表进行适当排序。 如果您确实希望保留列表中的项目为字符串的行为,则可以使用key参数并为其提供一个排序依据的函数:
listPressups.sort(key=int, reverse=True)
由于元素是字符串,因此当前按字典顺序进行排序。要将它们按数字格式化,必须将它们转换为数字。对于任何列表,您都可以按如下方式进行操作
arr = ['8', '75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10']
print(sorted(list(map(int,arr)),reverse=True))
输出:
所以,在你的情况下,你可以这样做
opener = open("gymclub.txt", "r")
reader = opener.readline()
listObjects = []
listNames = []
listPressups = []
listPullups = []
sortedListPressups = []
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = splitting[1]
pull_ups = splitting[2]
reader = opener.readline()
listPressups.append(press_ups)
sortedListPressups = (sorted(list(map(int,listPressups)),reverse=True))
print(sortedListPressups)
输出:
如果希望最终列表像最初一样是字符串列表本身,那么也可以按照以下方式进行
opener = open("gymclub.txt", "r")
reader = opener.readline()
listObjects = []
listNames = []
listPressups = []
listPullups = []
sortedListPressups = []
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = splitting[1]
pull_ups = splitting[2]
reader = opener.readline()
listPressups.append(press_ups)
sortedListPressups = sorted(listPressups, reverse = True, key = int)
print(sortedListPressups)
输出:
如果要就地排序,还可以执行sortedListPressups.sortreverse=True,key=int而不是使用sorted。您可以阅读有关这些方法的更多信息对字符进行排序将导致此结果。您需要将字符串元素强制转换为数字:
opener = open("gymclub.txt", "r")
reader = opener.readline()
listObjects = []
listNames = []
listPressups = []
listPullups = []
sortedListPressups = []
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = int(splitting[1])
pull_ups = splitting[2]
reader = opener.readline()
listPressups.append(press_ups)
sortedListPressups = sorted(listPressups, reverse = True)
print(sortedListPressups)
也许你可以分享gymclub.txt如何让其他人复制你的大小写。字符串按字典顺序排序,即按字母顺序排序
opener = open("gymclub.txt", "r")
reader = opener.readline()
listObjects = []
listNames = []
listPressups = []
listPullups = []
sortedListPressups = []
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = splitting[1]
pull_ups = splitting[2]
reader = opener.readline()
listPressups.append(press_ups)
sortedListPressups = sorted(listPressups, reverse = True, key = int)
print(sortedListPressups)
['75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10','8']
opener = open("gymclub.txt", "r")
reader = opener.readline()
listObjects = []
listNames = []
listPressups = []
listPullups = []
sortedListPressups = []
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = int(splitting[1])
pull_ups = splitting[2]
reader = opener.readline()
listPressups.append(press_ups)
sortedListPressups = sorted(listPressups, reverse = True)
print(sortedListPressups)