大家好。我最近在学习python。
我在按数字顺序排序文件时遇到问题。我在列表中按顺序排列文件:
["1card.txt", "card.txt" , "3card.txt", "52card.txt", "badcard.txt"]
当我只打印列表时,它不按顺序打印,而是打印:1card.txt, 10card.txt
等等。那我该如何修复以下代码呢?
file=glob.glob('/directory/*.txt')
sorted(file, key=int)
答案 0 :(得分:2)
怎么样:
import re
def tryint(s):
try:
return int(s)
except ValueError:
return s
def alphanum_key(s):
return [tryint(c) for c in re.split('([0-9]+)', s)]
def sort_nicely(l):
return sorted(l, key=alphanum_key)
然后你可以这样做:
>>> file = ["1card.txt", "card.txt" , "3card.txt", "52card.txt", "badcard.txt"]
>>> sort_nicely(file)
['1card.txt', '3card.txt', '52card.txt', 'badcard.txt', 'card.txt']
答案 1 :(得分:0)
没有正则表达式的简单解决方案可能是:
def sort_int(examp):
pos = 1
while examp[:pos].isdigit():
pos += 1
return examp[:pos-1] if pos > 1 else examp
sorted(files, key=sort_int)
['1card.txt','3card.txt','52card.txt','badcard.txt','card.txt']
答案 2 :(得分:0)
files = ["1card.txt", "card.txt" , "3card.txt", "52card.txt", "badcard.txt"]
def nat_sort(s):
'''
provides a sort mechanism for strings that may or
may not lead with an integer
'''
for i, c in enumerate(s):
if not c.isdigit():
break
if not i:
return 0, s
else:
return int(s[:i]), s[i:]
files.sort(key=nat_sort)
现在files
是一个排序列表:
['badcard.txt', 'card.txt', '1card.txt', '3card.txt', '52card.txt']
要对相似的字母进行排序,请按照与上述类似的方式进行:
def nat_sort(s):
'''
provides a sort mechanism for strings that may or
may not lead with an integer, but groups by strings
starting after integers, if any
'''
for i, c in enumerate(s):
if not c.isdigit():
break
if not i:
return s, 0
else:
return s[i:], int(s[:i])
files.sort(key=nat_sort)
现在文件返回:
['badcard.txt', 'card.txt', '1card.txt', '3card.txt', '52card.txt']
答案 3 :(得分:-3)
您可以通过调用列表的 sort()方法来整理列表:
>>> foo= ['spam', 'eggs', 'ggg']
>>> foo.sort()
['eggs', 'ggg', 'spam']