Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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 生成123的排列列表时发生内置函数错误_Python_Algorithm - Fatal编程技术网

Python 生成123的排列列表时发生内置函数错误

Python 生成123的排列列表时发生内置函数错误,python,algorithm,Python,Algorithm,我想制作一个123排列的排序列表,但我不能使用python的模块。当我运行此代码时,会收到以下消息: “内置函数”或“方法”对象没有“排序”属性(第22行) 这里有一个问题: elements = elements.split 我想你的意思是: elements = elements.split() # you forgot the parentheses! 还有一个类似的问题: list = list.sort # you forgot the parentheses, and the f

我想制作一个123排列的排序列表,但我不能使用python的模块。当我运行此代码时,会收到以下消息:

“内置函数”或“方法”对象没有“排序”属性(第22行)

这里有一个问题:

elements = elements.split
我想你的意思是:

elements = elements.split() # you forgot the parentheses!
还有一个类似的问题:

list = list.sort # you forgot the parentheses, and the function call is wrong
我想你在找这个:

list = sorted(list)
或者这个:

list.sort() # no assignment here, it's an in-place sort
还有一点建议:调用变量
列表是个坏主意,它与内置类型和函数名冲突。最后一句话:您想做的已经在标准模块中实现,请尝试一下:

import itertools as it
list(it.permutations([1, 2, 3])) # see? told you `list` was a function!
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

当然,也应该调用split,这就是OP得到特定错误的原因<代码>元素
成为内置方法
拆分
列表
因子
返回时分配给该元素,当然
拆分
没有属性
排序
。我一直在想到底是什么导致了这种情况,尽管排序错误是显而易见的。@ÓscarLópez非常感谢你。我编辑了代码,但再次出错。现在它说:“非类型”对象是不可编辑的(第24行)。@user3015255哪一个是第24行?@scarLópez list=sorted(list)噢,太遗憾了!30分钟前我第一次看到这个网站!:)我不知道!
import itertools as it
list(it.permutations([1, 2, 3])) # see? told you `list` was a function!
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]