按名称(str)属性按土耳其语字母顺序对类对象进行排序

2024-09-27 21:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在尝试对播放器类对象进行排序,其名称存储为属性,如下所示

class Player:
def __init__(self,license_no):
    self.license = license_no
    self.name = input('Oyuncunun adını ve soyadını giriniz: ').replace('ı','I').replace('i','İ').upper()
    self.fide_rating = get_ratings('Oyuncunun FIDE kuvvet puanını giriniz: ')
    self.national_rating = get_ratings('Oyuncunun ulusal kuvvet puanını giriniz: ')
    self.points = 0

我在一个列表中有这些对象,我想按它们的名称属性对列表进行排序。我可以和接线员一起做。加上英文字母顺序很好,但我需要按土耳其语字母进行排序,土耳其语字母有“Ç”、“Ö”等字符。我如何做到这一点?感谢您的帮助


Tags: 对象noself名称get属性排序license
3条回答

我相信我已经用这个名为PyICU的库解决了这个问题,这个库在这里使用:How do I sort unicode strings alphabetically in Python?

import icu # pip install PyICU


collator = icu.Collator.createInstance(icu.Locale('gr_GR'))

letters="abcçdefgğhıijklmnoöprsştuüvyz" #Turkish alphabet


import random

not_names = []

## creating fake names and players 
for i in range(15):
  not_names.append(''.join([random.choice(letters) for a in range(5)]))

class Player:
  def __init__(self,license_no, name):
    self.license = license_no
    self.name = name

players = []

for idx, name in enumerate(not_names):
  players.append(Player(idx, name))

for player in players: print(player.license, player.name)

players.sort(key=lambda d: collator.getSortKey(d.name)) ## sorting done here
print('\n')

for player in players: print(player.license, player.name)

你想做这样的事吗

class S:
  def __init__(self, name):
    self.name = name


c = [S('ç'), S('a')]

# we create a dictionary with the index of the current object in the c list as key
# and name as value
name_to_sort= {}
for ind, obj in enumerate(c):
  name_to_sort[ind]=getattr(obj, 'name')

# in here we sort this dict in base of the values and create a list of list with [index, value]
sorted_by_name = [[k, v] for k, v in sorted(name_to_sort.items(), key=lambda name_to_sort: name_to_sort[1])]

# then in here we create a sorted list by sorting the indexes in the sorted_by_name
# and getting the original object which belonged to that index
sort = []
for ind, val in enumerate(sorted_by_name):
  sort.append(c[val[0]])

# we print the new list by the name
for i in sort:
  print(i.name)

输出:

a
ç

另一种方法是使用str.translate方法,该方法在两组字符串之间构建转换表,如下所示:

>>> ascii_letters = 'abccdefgghiijklmnooprsstuuvyz'
>>> turkish_letters = 'abcçdefgğhıijklmnoöprsştuüvyz'
#Build the translation table, notice how I've added extra chars in second 
#string to account for special Turkish letters. Basically they should have 
#the same length
>>> turkishTable = str.maketrans(turkish_letters, ascii_letters)
>>> 
>>> s = 'Çınar'
>>> 
>>> s.lower().translate(turkishTable)
'cinar'
>>> player_list = ['Zeynep','Ahmet', 'Çınar', 'Oğuz']
>>> 

>>> sorted(player_list, key=lambda s: s.lower().translate(turkishTable))
['Ahmet', 'Çınar', 'Oğuz', 'Zeynep']

然后,您可以在Player类中添加另一个属性,并调用它,例如self.translated_name,在该类中,您使用该属性与operator.attrgetter一起保存翻译名称sort

>>> from operator import attrgetter
>>> sorted(list_of_player_object, key=attrgetter('translated_name'))

相关问题 更多 >

    热门问题