Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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/magento/5.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 列表的组合_Python_Python 3.x_List - Fatal编程技术网

Python 列表的组合

Python 列表的组合,python,python-3.x,list,Python,Python 3.x,List,我正在尝试获取列表的所有组合,但每次需要删除两个元素时,如何删除这些元素 我试着做了两次for循环,每次它都会删除两个元素,但最后它并没有恢复列表 indexes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for een in indexes: for twee in indexes: temp = indexes if een == twee: pass else:

我正在尝试获取列表的所有组合,但每次需要删除两个元素时,如何删除这些元素

我试着做了两次for循环,每次它都会删除两个元素,但最后它并没有恢复列表

indexes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for een in indexes:
    for twee in indexes:
        temp = indexes
        if een == twee:
            pass
        else:
            if een in temp:
                temp.remove(een)
            temp.remove(twee)

            print(temp)
        temp = indexes
我希望每次都输出一个长度为9的列表,但列表一直在变短。 我得到的结果是:

[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 3, 5, 6, 7, 8, 9, 10]
[2, 3, 5, 7, 8, 9, 10]
[2, 3, 5, 7, 9, 10]
[2, 3, 5, 7, 9]
[5, 7, 9]
[5, 9]
第一个列表是正确的,但在下一个列表中,1不会返回列表。我做错了什么?同样,在这一步完成之后,een应该等于1,然后再重复一遍,之后,een应该等于2。。。。。 这应该是输出

[2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 2, 4, 5, 6, 7, 8, 9, 10]
[0, 2, 3, 5, 6, 7, 8, 9, 10]
[0, 2, 3, 4, 6, 7, 8, 9, 10]
[0, 2, 3, 4, 5, 7, 8, 9, 10]
[0, 2, 3, 4, 5, 6, 8, 9, 10]
[0, 2, 3, 4, 5, 6, 7, 9, 10]
[0, 2, 3, 4, 5, 6, 7, 8, 10]
[0, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 1, 4, 5, 6, 7, 8, 9, 10]

在每个组合都达到之前,你应该用temp=索引替换temp=索引[:]

temp=索引不创建新的列表,看看预期的输出是什么?你到底想实现什么?我期望[2,3,4,5,6,7,8,9,10],[1,3,4,5,6,8,9,10],[1,2,4,5,6,7,8,9,10],[1,2,3,5,6,6,6,6,10]等等,当这一步完成时,它应该继续下一步[2,3,4,5,6,7,8,9,10],[0,3,4,5,6,7,8,9,10],[0,2,4,5,6,7,8,9,10]etc@Samuelvde:用temp=index[:]替换temp=index,您就可以开始了