如果我有一个称为t的多维列表,并且我将列表中的一些数字附加到一个称为TC的新列表中,那么如何将所有没有附加到新列表中的数字放入它们自己的列表中,称为nonTC?例如:
t = [[1, 3, 4, 5, 6, 7],[9, 7, 4, 5, 2], [3, 4, 5]]
我写了一些条件,只从每个列表中添加一些值来创建新的列表TC:
TC = [[3, 4, 6], [9, 7, 2], [5]]
如何将TC中未包含的值附加到它自己的列表中?所以我会得到:
nonTC = [[1, 5, 7],[4, 5],[3,4]]
Tags:
只是出于好奇,使用NumPy:
您可以使用列表理解和集合列表来筛选原始列表:
输出:
读数:
和:Explanation of how nested list comprehension works?
其他方法的建议,AChampion的信条:
请参见zip()——它本质上是将两个列表捆绑成一个元组的可数
多个事件的附加组件-没收列表组件和集合:
它只需要一个通过你的项目,而不是2(单独的列表comps),这使它可能更有效地从长远来看
相关问题 更多 >
编程相关推荐