我正在尝试以国际化的方式为地图生成街道索引,并为编号的街道添加“自然”排序顺序。只要数字仅使用ASCII数字,但使用其他脚本中的数字(例如,波斯语在这里,1 = 1和2 = 2
#! /usr/bin/python3
import locale
from natsort import natsort_keygen, ns
locale.setlocale(locale.LC_ALL, 'fa_IR.UTF-8')
streets = [
"2 street",
"1 street",
"12 street",
"11 street",
"۲ street",
"۱ street",
"۱۲ street",
"۱۱ street",
]
sl = sorted(streets, key = natsort_keygen(alg=ns.LOCALE|ns.IGNORECASE|ns.TYPESAFE))
for name in sl:
print(name)
预期结果:
1 street
۱ street
2 street
۲ street
11 street
12 street
۱۱ street
۱۲ street
实际结果:
۱۱ street
۱۲ street
1 street
۱ street
2 street
۲ street
11 street
12 street
答案 0 :(得分:0)
Python @iterations: 10;
.rate-loop (@i) when (@i > 0) {
.rate-@{i} {
width: percentage((@i / @iterations));
}
.rate-loop(@i - 0.1);
}
.rate-loop (@iterations);
能够正确转换,因此您可以自己定位:
int()
这会给你:
def natsorted(l):
# For simple string lists containing numbers
return sorted(l, key=lambda x: [int(d) if d else l for d, l in re.findall(r'(\d+)|(\D+)', x)])
streets = [
"2 street",
"1 street",
"12 street",
"11 street",
"۲ street",
"۱ street",
"۱۲ street",
"۱۱ street",
]
sl = natsorted(streets) #, key = natsort_keygen(alg=ns.LOCALE|ns.IGNORECASE|ns.TYPESAFE))
for name in sl:
print(name)