Powerbi 也是 microsoft product 所以我认为对于类似于 Explorer 的排序,它也使用 API StrCmpLogicalW() 进行这种排序(称为'natural sort order') .
一个很好的解释可以找到here。
import numpy as np
import pandas as pd
import re
df = pd.DataFrame({
'Subworkload': ['R', 'W10', 'W11', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9'],
'Iteration': np.zeros(11),
'Parameters': ['Buffer_size=Default_Num_Threads=1' for i in range(11)]
})
def sort_nicely( l ):
""" Sort the given list in the way that humans expect."""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
l.sort( key=alphanum_key )
return l
val = sort_nicely(list(df['Subworkload']))
print(val)
:输出:
'R', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10', 'W11']
但根据StrCmpLogicalW 输出应该是:
'R', 'W10', 'W11', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9']