假设我们有一个像这样的字符串列表
a = ['AA20', 'AB20', 'AC20', 'AD20','B20', 'BA20', 'BB20', 'BC20', 'BD20', 'C20', 'D20', 'E20', 'F20', 'G20', 'H20', 'I20', 'J20', 'K20', 'L20', 'M20', 'N20', 'O20', 'P20', 'Q20', 'R20', 'S20', 'T20', 'U20', 'V20', 'W20', 'X20', 'Y20', 'Z20']
我需要对列表进行排序,使输出看起来像这样,
['B20','C20','D20', 'E20', 'F20', 'G20', 'H20','I20', 'J20', 'K20', 'L20', 'M20', 'N20', 'O20', 'P20', 'Q20', 'R20', 'S20', 'T20', 'U20', 'V20', 'W20', 'X20','Y20', 'Z20', 'AA20', 'AB20', 'AC20', 'AD20']
我的代码
print(sorted(a, key=str.lower))
如何按上述方式对列表进行排序?
答案 0 :(得分:0)
希望这会有所帮助:)
a = ['AA20', 'AB20', 'AC20', 'AD20','B20', 'BA20', 'BB20', 'BC20', 'BD20', 'C20', 'D20', 'E20', 'F20', 'G20', 'H20', 'I20', 'J20', 'K20', 'L20', 'M20', 'N20', 'O20', 'P20', 'Q20', 'R20', 'S20', 'T20', 'U20', 'V20', 'W20', 'X20', 'Y20', 'Z20']
print(sorted(a, key=len))