Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python str.replace不获取参数_Python_Sorting_Arguments - Fatal编程技术网

python str.replace不获取参数

python str.replace不获取参数,python,sorting,arguments,Python,Sorting,Arguments,我正在使用key=函数对列表进行排序,我正在对用LaTeX编写的歌曲标题列表进行排序 songs = sorted(songs, key=str.replace('$\lambda$','lambda')) 问题是,当函数运行时,它会说:“TypeError:replace()至少接受两个参数(得到一个)您的关键参数应该是可调用的。您现在正在发送字符串,这就是错误 你可以试试这个: songs = sorted(songs, key=lambda s: s.replace('$\lambda$

我正在使用key=函数对列表进行排序,我正在对用LaTeX编写的歌曲标题列表进行排序

songs = sorted(songs, key=str.replace('$\lambda$','lambda'))

问题是,当函数运行时,它会说:“TypeError:replace()至少接受两个参数(得到一个)

您的关键参数应该是可调用的。您现在正在发送字符串,这就是错误

你可以试试这个:

songs = sorted(songs, key=lambda s: s.replace('$\lambda$','lambda'))
用于就地分拣

songs.sort(key=lambda s: s.replace('$\lambda$','lambda'))

或者使用
functools
中的
partial
,速度更快,但需要导入。