【发布时间】:2023-04-01 00:08:01
【问题描述】:
我有一个棘手的问题,我很难解决问题。
我有两个列表:
firstList = [[0, 9], [0, 4], [0]]
secondList = [[18], [19, 7], [20]]
我想以升序从secondList 中的值中减去firstList 中的值,但仅如果firstList 中的值尚未“使用” ”。例如:
thirdList = [0,4]
fourthList = [19,7]
emptyList = []
emptyList.append(fourthList-thirdList]
print(emptyList)
>>>[7,3]
在这种情况下,19 没有被使用,因为在thirdList 中的前一个值和19 之间没有任何值。
我在想这样的事情(虽然它很快分解成伪代码)
firstList = [[0, 9], [0, 4], [0]]
secondList = [[18], [19, 7], [20]]
emptyList = [ [] for y in range(3) ]
for x in range(3) :
#for smallest value in secondList[x], subtract all smaller values in firstList and then delete them, append values to emptyList[x]
#for next smallest value in secondList[x], subtract all smaller values in firstList and then delete them, append values to emptyList[x]
#repeat until firstList is empty, then exit loop
print(emptyList)
>>>[[9, 18], [3, 7], [20]]
这将排除在 secondList[1] 中使用19,因为0 和4 在从7 中减去后已经被删除
【问题讨论】:
标签: python for-loop conditional-statements nested-lists subtraction