鱼C论坛

 找回密码
 立即注册
查看: 2219|回复: 2

[已解决]请教排序问题

[复制链接]
发表于 2017-9-9 01:10:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Block111 于 2017-9-9 16:30 编辑

需要按照最后一列排序;怎么只排序四位数的几项,后面都不能排序;
#排序
[b]def select_sort(array):
    p=list(open(array+'.txt','r'))   
    t_array=[]
    for each in p:
        l=int(each.split(',',4)[4])
        t_array.append(each)      
    for i in range(len(t_array)-1):
        min = int(i)
        for j in range(i+1, len(t_array)):
            if t_array[j] < t_array[min]:
                 min = int(t_array[j])
        p[i], p[min] = p[min], p[i]
    file=open(array+'x.txt','w')
    file.writelines(p)
    file.close()


排序后结果:
140301-MSHA-S1303P,1,GR-A,HP140X8,2264
140301-MSHA-S1304P,1,GR-A,HP140X8,2182
140301-MSHA-S1305P,1,GR-A,HP140X8,2182
140301-MSHA-S1312P,1,GR-A,HP140X8,7800
140301-MSHA-S1313P,1,GR-A,HP140X8,7800
140301-MSHA-S1314P,1,GR-A,HP140X8,7800
140301-MSHA-S1315P,1,GR-A,HP140X8,7800
140301-MSHA-S1316P,1,GR-A,HP140X8,7800
140301-MSHR-S1300S,1,GR-A,HP140X8,2182
140301-MSHR-S1301S,1,GR-A,HP140X8,2182
140301-MSHR-S1302S,1,GR-A,HP140X8,2264
140301-MSHR-S1307S,1,GR-A,HP140X8,7800
140301-MSHR-S1308S,1,GR-A,HP140X8,7800
140301-MSHR-S1309S,1,GR-A,HP140X8,7800
140301-MSHR-S1310S,1,GR-A,HP140X8,7800
140301-MTTA-S589P,1,GR-A,HP140X8,7800
140301-MTTA-S590P,1,GR-A,HP140X8,7800
140301-MTTA-S591P,1,GR-A,HP140X8,7800
140301-MTTA-S592P,1,GR-A,HP140X8,7800
140301-MTTR-S584S,1,GR-A,HP140X8,7800
140301-MTTR-S585S,1,GR-A,HP140X8,7800
140301-MTTR-S586S,1,GR-A,HP140X8,7800
140301-MTTR-S594S,1,GR-A,HP140X8,7800
140301-WSH-S1306S,1,GR-A,HP140X8,7800
140301-WSH-S1311S,1,GR-A,HP140X8,7800
140301-WSH-S1317P,1,GR-A,HP140X8,7800
140301-WTT-S1318S,1,GR-A,HP140X8,650
140301-WTT-S1319S,1,GR-A,HP140X8,650
140301-WTT-S1320S,1,GR-A,HP140X8,725
140301-WTT-S1321P,1,GR-A,HP140X8,725
140301-WTT-S1322P,1,GR-A,HP140X8,650
140301-WTT-S1323P,1,GR-A,HP140X8,650
最佳答案
2017-9-9 09:52:07
2017-09-09_095141.png

  1. def select_sort(array):
  2.     p=list(open(array+'.txt','r'))
  3.     t_array=[]
  4.     for each in p:
  5.         l=int(each.split(',',4)[4])
  6.         t_array.append([l,each])
  7.    
  8.     t_array.sort()
  9.    
  10.     content = ''
  11.     for each in t_array:
  12.         content += each[1]
  13.         
  14.    
  15.     file=open(array+'x.txt','w')
  16.     file.writelines(content)
  17.     file.close()


  18. select_sort('1111')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-9 09:52:07 | 显示全部楼层    本楼为最佳答案   
2017-09-09_095141.png

  1. def select_sort(array):
  2.     p=list(open(array+'.txt','r'))
  3.     t_array=[]
  4.     for each in p:
  5.         l=int(each.split(',',4)[4])
  6.         t_array.append([l,each])
  7.    
  8.     t_array.sort()
  9.    
  10.     content = ''
  11.     for each in t_array:
  12.         content += each[1]
  13.         
  14.    
  15.     file=open(array+'x.txt','w')
  16.     file.writelines(content)
  17.     file.close()


  18. select_sort('1111')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-9 11:12:16 | 显示全部楼层
本帖最后由 Block111 于 2017-9-9 16:28 编辑

Thanks a million!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-2 05:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表