Python 3.7 使用sorted()函数对列表进行不区分大小写排序时出现TypeError: lower() takes no arguments (1 given)

在使用sorted()函数做练习时,分别定义了两个函数ording1()和ording2(),区别是ording1()中用于排序的列表是通过split()函数生成的,ording2()函数中的列表是直接用的字面值。最终都是使用sorted(str_list, key=str.lower) 函数对
list进行排序,执行ording2()时,系统正常输出,但是执行以ording1()时却遇到了以下错误信息:TypeError: lower() takes no arguments (1 given),不太理解这是什么原因导致的。

def ording1():
    str = "This is a test string from Andrew"
    str_list = str.split()
    print(type(str_list))
    print(str_list)
    print(sorted(str_list, key=str.lower))
def ording2():
    str_list = ['This', 'is', 'a', 'test', 'string', 'from', 'Andrew']
    print(type(str_list))
    print(str_list)
    print(sorted(str_list, key=str.lower))

执行ording1()函数时出现以下错误

print(sorted(str_list, key=str.lower))
TypeError: lower() takes no arguments (1 given)

执行ording1()函数时,显示正常

<class 'list'>
['This', 'is', 'a', 'test', 'string', 'from', 'Andrew']
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

因为你有一个变量叫做str,冲突了

修改为

def ording1():
    str1 = "This is a test string from Andrew"
    str_list = str1.split()
    print(type(str_list))
    print(str_list)
    print(sorted(str_list, key=str.lower))

问题解决的话,请点下采纳