如何在不更改原始列表的情况下使用排序键
how to use sorted key without changing the origonal list
我正在尝试找到一种方法来根据每个名称的每个字符的值的总和对列表进行排序,到目前为止,我得到的最好的方法如下。我不知道如何在保留列表的同时对列表进行排序,但输出最终是序号。我尝试使用复制的列表并从该列表中获取数字,但后来我不知道如何使用它对原始列表进行排序。
我想尝试函数内的大部分代码,以便它与其他输入(总和、长度、名字、姓氏)一起工作。
相关代码在这里:
def namefunction()
global namelist
for i in range(0,len(namelist)):
namelist[i]=list(namelist[i])
for x in range(0, len(namelist[i])):
namelist[i][x]=ord(namelist[i][x])
return sum
print(*sorted(namelist, key= namefunction()), sep='\n')
这里有完整代码供参考(不是很需要看)
print('You can sort by first name, last name, length, sum, or words .')
keycheck=input("What do you want to sort by? ")
namelist=['John S. Zmile', 'Giorno Giovana',
'Kakyoin Hanagashi', 'Trilaw Flanger', 'Stephane Locost',
'Bobert Hobert', 'Volvo B10BLE', 'Dale Reid', 'Annika Sörenstam',
'Cryt Fider', 'Valentine Prinsep', 'Gamrekeli Tony Toreli']
def namefunction():
if keycheck=="firstname":
return
elif keycheck=="last name":
return lambda x:x[x.rfind(' ')+1]
elif keycheck=="length":
return len
elif keycheck=="words":
return lambda x: -len(x.split())
elif keycheck =="sum":
global namelist
for i in range(0,len(namelist)):
namelist[i]=list(namelist[i])
for x in range(0, len(namelist[i])):
namelist[i][x]=ord(namelist[i][x])
return sum
print(*sorted(namelist, key= namefunction()), sep='\n')
如果我很清楚,您想按字符的总和对字符串列表进行排序。
def value(char):
""" Return the position of the character in the alphabet. You could replace it by ord."""
if not char.isalpha():
return 0
return ord(char.lower()) - 96
def namefunction():
if keycheck == "firstname":
return lambda x: x.split()[0]
elif keycheck == "last name":
return lambda x: x.split(" ")[-1]
elif keycheck == "length":
return len
elif keycheck == "words":
return lambda x: len(x.split())
elif keycheck == "sum":
return lambda x: sum(map(value, x))
key = namefunction()
print(*sorted(namelist, key=key), sep='\n')
我正在尝试找到一种方法来根据每个名称的每个字符的值的总和对列表进行排序,到目前为止,我得到的最好的方法如下。我不知道如何在保留列表的同时对列表进行排序,但输出最终是序号。我尝试使用复制的列表并从该列表中获取数字,但后来我不知道如何使用它对原始列表进行排序。
我想尝试函数内的大部分代码,以便它与其他输入(总和、长度、名字、姓氏)一起工作。
相关代码在这里:
def namefunction()
global namelist
for i in range(0,len(namelist)):
namelist[i]=list(namelist[i])
for x in range(0, len(namelist[i])):
namelist[i][x]=ord(namelist[i][x])
return sum
print(*sorted(namelist, key= namefunction()), sep='\n')
这里有完整代码供参考(不是很需要看)
print('You can sort by first name, last name, length, sum, or words .')
keycheck=input("What do you want to sort by? ")
namelist=['John S. Zmile', 'Giorno Giovana',
'Kakyoin Hanagashi', 'Trilaw Flanger', 'Stephane Locost',
'Bobert Hobert', 'Volvo B10BLE', 'Dale Reid', 'Annika Sörenstam',
'Cryt Fider', 'Valentine Prinsep', 'Gamrekeli Tony Toreli']
def namefunction():
if keycheck=="firstname":
return
elif keycheck=="last name":
return lambda x:x[x.rfind(' ')+1]
elif keycheck=="length":
return len
elif keycheck=="words":
return lambda x: -len(x.split())
elif keycheck =="sum":
global namelist
for i in range(0,len(namelist)):
namelist[i]=list(namelist[i])
for x in range(0, len(namelist[i])):
namelist[i][x]=ord(namelist[i][x])
return sum
print(*sorted(namelist, key= namefunction()), sep='\n')
如果我很清楚,您想按字符的总和对字符串列表进行排序。
def value(char):
""" Return the position of the character in the alphabet. You could replace it by ord."""
if not char.isalpha():
return 0
return ord(char.lower()) - 96
def namefunction():
if keycheck == "firstname":
return lambda x: x.split()[0]
elif keycheck == "last name":
return lambda x: x.split(" ")[-1]
elif keycheck == "length":
return len
elif keycheck == "words":
return lambda x: len(x.split())
elif keycheck == "sum":
return lambda x: sum(map(value, x))
key = namefunction()
print(*sorted(namelist, key=key), sep='\n')