如何根据提供的信件对此进行排序?这封信是'A'
。所以:
Input: 'Sonu Kumar,Rajesh Jo,Bhai Sahab,RH,Rk Aju,Abhi KK,Goharjha Ara,Avinash Kumar'
Required Output: 'Avinash Kumar,Abhi KK,Goharjha Ara,Rk Aju,Rajesh Jo,Bhai Sahab,Sonu Kumar'
要对具有以下优先级的名称列表进行排序:
应省略不包含字母的名称。 应该使用严格的1个数组。欢迎任何其他算法。如果可以,请把它变成pythonic。
以下是我的代码:
#letter
letter='A'
#name list
names='Sonu Kumar,Rajesh Jo,Bhai Sahab,RH,Rk Aju,Abhi KK,Goharjha Ara,Avinash Kumar'
#fitting them in an array, and removing the names which do not contain the letter
ordered=[j for j in names.strip(' ').split(',') if letter.lower() in j.lower()]
#to sort names according to the appearance order of letter
def FSort(elem):
return elem.lower().find(letter.lower())
#to sort names so that second names starting with provided letter comes first
def SSort(elem):
return elem.find(' '+letter)
#to sort names so that first names starting with provided letter comes first
def TSort(elem):
return elem.lower().find(letter)
ordered=sorted(ordered,key=FSort)
ordered=sorted(ordered,key=SSort) #this is not working as expected
ordered=sorted(ordered,key=TSort) #final output can be a list, here ordered