我正在编写代码来计算创建列表中的其他部分(例如2x2 = 2 1x1)所需的乐高件数量,并且我有这部分工作。但是,我还要在此之后返回一个新列表,并将其结合起来。删除已使用或组合的乐高积木的过程(因此,不再可用)。我现在的代码剥离了lego的所有实例,而不仅仅是那些使用过的'。我想知道是否有办法从列表中删除某个项目的次数。提前致谢。
import hw4_util
legos = hw4_util.read_legos()
print 'Current legos',legos
def lego_work(lego,q,leglist):
count=0
num1x1= leglist.count('1x1')
num2x1= leglist.count('2x1')
num2x2= leglist.count('2x2')
num4x2= leglist.count('4x2')
if lego =='1x1':
count = num1x1
elif lego == '2x1':
if num2x1 == 0:
count = (num1x1/2)
leglist2= filter(lambda a: a != '1x1', leglist)
else:
count= num2x1
leglist2=filter(lambda a: a != '2x1', leglist)
elif lego =='2x2':
if num2x2==0 and (num2x1/2) ==0:
count = (num1x1/4)
leglist2= filter(lambda a: a != '1x1', leglist)
elif num2x2==0 and num2x1>0:
count= num2x1
leglist2= filter(lambda a: a != '2x1', leglist)
else:
count=num2x2
leglist2= filter(lambda a: a != '2x2', leglist)
elif lego== '4x2':
if num4x2==0 and (num2x2/2) and (num2x1/4)==0:
count = (num1x1/8)
leglist2= filter(lambda a: a != '1x1', leglist)
elif num4x2==0 and num2x2==0 and num2x1>0:
count= num2x1
leglist2= filter(lambda a: a != '2x1', leglist)
elif num4x2==0 and num2x2>0:
count=num2x2
leglist2= filter(lambda a: a != '2x2', leglist)
else:
count=num4x2
leglist2= filter(lambda a: a != '4x2', leglist)
if count>=q:
print 'You can use',q,lego,'legos for this.'
elif count<q:
print " I don't have",q,lego,'legos.'
print
print 'Current legos:',leglist2
legotype= raw_input('Type of lego wanted =>')
print legotype
quantity= input('Quantity wanted =>')
print quantity
print lego_work(legotype,quantity,legos)
答案 0 :(得分:0)
您可以使用leglist.remove('2x1')
从列表中删除其中一个部分。