Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Sorting_Tuples - Fatal编程技术网

Python基于元组值删除元组

Python基于元组值删除元组,python,sorting,tuples,Python,Sorting,Tuples,我想返回元组,这样它们加起来的权重就可以达到400,但也不应该有重复的类 因此,下面的最佳解决方案是(items,weight,以下是如何从列表中删除元素: >>> a = [1, 2, 3] >>> a [1, 2, 3] >>> a.pop() #remove & return the right-most element 3 >>> a [1, 2] >>> a.pop(0) #remove

我想返回元组,这样它们加起来的权重就可以达到400,但也不应该有重复的类


因此,下面的最佳解决方案是(items,weight,以下是如何从列表中删除元素:

>>> a = [1, 2, 3]
>>> a
[1, 2, 3]
>>> a.pop()  #remove & return the right-most element
3
>>> a
[1, 2]
>>> a.pop(0) #remove & return element at given index
1
>>> a
[2]

至于你的问题,它是一个变种,它是NPhard的。

你可能想考虑使用A或A(以类为键),而不是元组元组。它们更容易操作。

这个问题本身就是背包问题,这是一个经典的计算机科学,在互联网上有详尽的记录


最简单(但通常不准确)的解决方案是贪婪算法,但由于这几乎肯定是一个家庭作业问题,因此您需要动态规划解决方案-因为这是动态规划的规范介绍,具有可读的伪代码和漂亮的表。

我已根据类对元组进行排序,
排序(a,key=lambda t:t[3],reverse=True)
我现在不知道如何弹出它们。您现在有一个元组。尝试使用元组列表,这样更有意义。然后您可以执行类似..mylist.pop():)
>>> a = [1, 2, 3]
>>> a
[1, 2, 3]
>>> a.pop()  #remove & return the right-most element
3
>>> a
[1, 2]
>>> a.pop(0) #remove & return element at given index
1
>>> a
[2]