new_list=("a","g","x","e","s","s") #created a list
a=new_list.sort() #try to sort it
,错误是:
AttributeError Traceback (most recent call last)
<ipython-input-3-6eb33c65fab6> in <module>()
----> 1 a=new_list.sort()
AttributeError: 'tuple' object has no attribute 'sort'
我收到此属性错误。我已经重新启动内核,并在sublime-text-CMD上进行了尝试。我仍然遇到相同的错误
答案 0 :(得分:1)
new_list
被定义为tuple
。通过将其括在方格中使其成为list
括号
new_list=["a","g","x","e","s","s"]
new_list.sort()
print (new_list)
# ['a', 'e', 'g', 's', 's', 'x']
答案 1 :(得分:0)
元组是不可变的Python对象的序列,因此,您无法更改它们的值。也许您打算使用sorted而不是sort?
new_list=("a","g","x","e","s","s")
a = sorted(new_list)
答案 2 :(得分:0)
如果您在此处"Tuples and Sequences"看到,则可以看到您的数据结构是一个元组。
您可以在"More on Lists"处看到sort()
仅适用于数组。
您可以使用sorted()对数组或元组进行排序
new_list=("a","g","x","e","s","s")
a=sorted(new_list) # ['a', 'e', 'g', 's', 's', 'x']