按升序排序字段,并删除第一个和最后一个数字 [英] sort a field in ascending order and delete the first and last number

查看:132
本文介绍了按升序排序字段,并删除第一个和最后一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像以下数据:

I have a data that looks like following:

a     10,5,3,66,50
b     2,10,1,88,5,8,9
c     4,60,10,39,55,22
d     1,604,3,503,235,45,60,7
e     20,59,33,2,6,45,36,34,22

我想按升序排列在第二列中的数据进行排序

I want to sort the data in second column in ascending order

a     3,5,10,50,66
b     1,2,5,8,9,10,88
c     4,10,22,39,55,60
....
....

然后删除最小,并从它的最大价值。所以像这样的:

Then delete the smallest and the largest value from it. So like this:

a     5,10,50
b     2,5,8,9,10
c     10,22,39,55
....
....

任何帮助将是AP preciated!

Any help would be appreciated!

推荐答案

的Python:

with open('the_file.txt', 'r') as fin, open('result.txt', 'w') as fout:
    for line in fin:
        f0, f1 = line.split() 
        fout.write('%s\t%s\n' % (f0, ','.join(sorted(f1.split(','), key=int)[1:-1])))

的循环体可以解压为:

The body of the loop can be unpacked as:

        f0, f1 = line.split()           # split fields on whitespace
        items = f1.split(',')           # split second field on commas
        items = sorted(items, key=int)  # or items.sort(key=int) # sorts items as int
        items = items[1:-1]             # get rid of first and last items
        f1 = ','.join(items)            # reassemble field as csv
        line = '%s\t%s\n' % (f0, f1)    # reassemble line
        fout.write(line)                # write it out

这篇关于按升序排序字段,并删除第一个和最后一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆