如何处理输出文件区分大小写的排序? [英] How to deal with case sensitive sorting for output files?

查看:89
本文介绍了如何处理输出文件区分大小写的排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序在csv的第1列中随机生成一系列5个字母(ASCII,大写和小写),在csv的第2列中随机生成4个数字(0-9),并将它们另存为文件.我可以按升序对第2列进行排序,但是由于第1列首先对所有大写值然后对小写进行了排序,因此我很难与第1列进行比较.这也会输出到一个新文件('sorted.csv')

I have written a program that randomly generates a series of 5 letters (ASCII, both upper and lower case) in column 1 of a csv and 4 numbers (0-9) in column 2 of a csv and saves them as a file. I can sort column 2 in order of ascending values but struggle with the column 1 as it sorts all the upper case values first and then lower case. this is also output to a new file ('sorted.csv')

示例:

ANcPI
DLBvA
FpSCo
beMhy
dWDjl

有人知道如何对它们进行排序,以使大写或小写不影响字母,而仅影响字母?它应该排序为:

does anyone know how to sort these so that upper or lower case does not impact but rather just the letter? It should sort as:

ANcPI
beMhy
DLBvA
dWDjl
FpSCo

推荐答案

我最近也遇到了这个问题,并且可以通过指定可选的key参数非常简单地解决-假设您的数据在列表中:

I ran into this recently as well, and it - assuming your data is in a list - can be solved very simply by specifying the optional key argument:

li = ['ANcPI', 'DLBvA', 'FpSCo', 'beMhy', 'dWDjl']
li.sort(key=lambda m : m.lower())

然后

>>>print(li)
['ANcPI', 'beMhy', 'DLBvA', 'dWDjl', 'FpSCo']

这篇关于如何处理输出文件区分大小写的排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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