English 中文(简体)
Wrong 匈牙利人(胡-胡)
原标题:Wrong Hungarian (hu_HU) sort order
import locale
locale.setlocale(locale.LC_COLLATE, hu_HU.ISO8859-2 )
print(sorted([ c ,  á ,  b ,  z ,  é ,  a ,  d ,  e ,  f ,  Ő ,  Ű ,  ő ,  ű ], key=locale.strxfrm))

预期:

Actual: [ a , á , b , c , d , e , é , f , z , Ő , ő , Ű , ű ]

Note that z is supposed to be the last letter.

我知道我的“工作”法,因为它确实将“工作”放在了正确的位置(而且“正常”在z 之后加以分类),因此,我的说法是地方性的。

I have MacOS Venture 13.6.4, python 3.11.7

我如何能够“更新”当地的定义? 它是在座的,还是使用当地制度?

注:我试图安装PyICU和zope.ucol,但两者在安装期间都失败,因此没有告诉我使用这些装置。

问题回答

语言敏感的分类是一个复杂的问题:

有三个办法:

  1. Use PyICU
  2. Use a locale based sort key
  3. Create your own function to process the sort keys

<>PyICU:

如果贵方守则需要在一个以上业务系统上开展工作,那么团结互助会是最佳的跨平台解决办法。

import icu
collator = icu.Collator.createInstance(icu.Locale( hu_HU ))
data = [ c ,  á ,  b ,  z ,  é ,  a ,  d ,  e ,  f ,  Ő ,  Ű ,  ő ,  ű ]
print(sorted(data, key=collator.getSortKey))
# [ a ,  á ,  b ,  c ,  d ,  e ,  é ,  f ,  ő ,  Ő ,  ű ,  Ű ,  z ]

但是,欧佩集团宁愿引导这一解决办法。

www.un.org/Depts/DGACM/index_spanish.htm

不同平台和校准执行的结果可能因当地使用而有所不同。

基本法典是:

import locale
locale.setlocale(locale.LC_COLLATE, hu_HU.UTF-8 )
data = [ c ,  á ,  b ,  z ,  é ,  a ,  d ,  e ,  f ,  Ő ,  Ű ,  ő ,  ű ]
print(sorted(data, key=locale.strxfrm))

成果各不相同。 macOS不支持匈牙利的串通。 不使用glibc的六氯环己烷分布不会支持匈牙利的串通。 我没有把Windows列入下表,但会正确工作。

Alpine: [ a ,  b ,  c ,  d ,  e ,  f ,  z ,  á ,  é ,  Ő ,  ő ,  Ű ,  ű ]
Freebsd: [ a ,  á ,  b ,  c ,  d ,  e ,  é ,  f ,  ő ,  Ő ,  ű ,  Ű ,  z ]
macOS:  a ,  b ,  c ,  d ,  e ,  f ,  z ,  á ,  é ,  Ő ,  ő ,  Ű ,  ű ]
Ubuntu: [ a ,  á ,  b ,  c ,  d ,  e ,  é ,  f ,  ő ,  Ő ,  ű ,  Ű ,  z ]

<<>Custom>

另一种做法是,在使用敏感度和案件不敏感的关键时,写出一种习惯功能:

import unicodedata as ud
import regex
data = [ c ,  á ,  b ,  z ,  é ,  a ,  d ,  e ,  f ,  Ő ,  Ű ,  ő ,  ű ]
# accent and case insensitive
def ai_si(x):
    xn = ud.normalize("NFD", x.casefold())
    xn = regex.sub(r p{Mn} ,   , xn)
    return (xn, x.swapcase())
print(sorted(data, key=ai_si))

。 如果回到了<条码>x,则单体和二字体将被完全忽略,这意味着其最终位置与其在名单上的地位相对应。 页: 1

我们可以将<条码>x或<条码>x.swapcase()作为附件的第二个要素。 <代码>x将形成一种顺序,在较低类别之前通常(但并不总是)对上级特性进行分类。 虽然x.swapcase()扭转了案件之间的相对次序。 还有其他办法,可以构建图人的第二个要素。

ai_si(>允许对二英和个案作二次区分。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签