Python比较/评估小数列表

2024-04-20 04:48:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python编写一个API,它接收小数列表,例如:

cutList = [3.00, 5.20, 2.50]

现在这些小数是剪切大小,我的API需要测试这些剪切大小是否有存货。因此,我的API查询所有当前的库存大小,并返回如下内容:

stockList = [9.00, 3.00]

我现在需要做的是评估两个列表,看看cutList和stockList中的所有值

我不能把所有的列表加在一起,因为这样计算是不正确的,例如(下面的计算是正确的,但是一个8.00的削减,不会来自两个4.00的削减(例如)

cutList = [8.00]
stockList = [4.00, 4.00]

但在我上面的例子中,3.00+5.20可能来自9.002.50可能来自3.00。所以我不想知道/问的是让python执行这些计算的最佳方法,有没有库可以做到这一点?你知道吗

我看过numPy,但我真的不知道我要做什么的专业术语,因此为什么我的谷歌搜索可能不会返回正确的信息。你知道吗

我可以通过一些杂乱的for/if语句来实现这一点,但我想检查一下,没有一个更干净的(或已发布的库)可用于此。你知道吗


Tags: 方法numpyapi信息内容列表for库存
1条回答
网友
1楼 · 发布于 2024-04-20 04:48:39

长话短说,我决定推出自己的,有一个numPy库的组合,我可以使用(主要是numpywhere),但我的列表的大小,这符合法案。你知道吗

注意,我决定扩展我的演示数据,以包括所需切割的订单行编号,以及返回带有工件编号(库存编号)的库存列表,这样当找到匹配项时,我就可以设置一个单独的dict,指出哪些切割来自哪些库存卷。然后它也会返回当前的库存水平,在所有客户选择的削减已经采取我不认为它会赢得我任何奖品,但它的工作(我认为)。欢迎提出任何意见/建议。你知道吗

from decimal import *

# Dummy Data
cutList = [Decimal(5.22), Decimal(3.55), Decimal(3.00), Decimal(9.00)]
stockList = [Decimal(5.25), Decimal(3.55), Decimal(3.10)]
y = 156709
orderline_no = 0
cutListDec = []
stockListDec = []

for x in cutList:
    orderline_no = orderline_no +1
    cutListDec.append({'orderline_no': orderline_no, 'size': x})

for x in stockList:
    y = y +1
    stockListDec.append({'piece_no': y, 'size': x})

# Now we need to check if all cuts from cut List can be taken
# from all cuts in stockList

# Find largest array in sequence
def find_largestDec(sequence):
    """return the minimum element of sequence"""
    low = sequence[0]['size']
    for i in sequence:
        if i['size'] > low:
            low = i['size']
            line_no = i['orderline_no']
        else:
            line_no = i['orderline_no']
            low = i['size']
    return {'orderline_no': line_no, 'size': low}


# find nearest value in array
def find_nearest(arrayDict, value, cutList):
    array = []
    for item in arrayDict:
        array.append(item['size'])
    n = [abs(i-value['size']) for i in array if i >= value['size']]
    if n == []:
        return None
    else:
        idx = n.index(min(n))
        # Find piece number for matching size found
        rowFind = next((x for x in arrayDict if x['size'] == array[idx]), None)
        return {'piece_no': rowFind['piece_no'], 'size': array[idx]}

def setAllocationStatus(listItem, stockAllocation, allocationList):
    allocations.append({
            'orderline_no': listItem['orderline_no'],
            'size': listItem['size'],
            'allocation': stockAllocation
        })
    return 

allocations = []
# Run the loop for as many items in list
for _ in range(len(cutListDec)):
    # Find biggest cut from user
    largestCustomerCut = find_largestDec(cutListDec)
    # Find the nearest useable piece that matches this
    largestStockCutAllocation = find_nearest(stockListDec, largestCustomerCut, cutListDec)

    if largestStockCutAllocation != None:
        # if satisfied, it means there is a useable piece
        # subtract the size of largestCut from largestCutAllocation
        # inside the stockList dict

        # this is the stockCut that the users cut will come from
        # largestCutAllocation this will be the new size of the stock, after the users cut
        newStocksize = largestStockCutAllocation['size'] - largestCustomerCut['size']
        newSizeStockRow = {'piece_no': largestStockCutAllocation['piece_no'], 'size': newStocksize}
        # tempoary new list 
        newStockList = []
        newCutList = []
        # loop over all stock
        for x in stockListDec:
            # if stock item matches the chosen allocation
            if x['size'] == largestStockCutAllocation['size']:
                # add thew new reduced size
                newStockList.append(newSizeStockRow)
            else:
                newStockList.append(x)

        for x in cutListDec:
            if x['orderline_no'] != largestCustomerCut['orderline_no']:
                # current item does not match chosen
                # add back into list
                newCutList.append(x)
            else:
                # cut does match, mark the allocation
                setAllocationStatus(x, largestStockCutAllocation['piece_no'], allocations)
        # update original arrays
        cutListDec = newCutList
        stockListDec = newStockList
    else:
        # No pieces big enough for this cut
        newCutList = []
        for x in cutListDec:
            # remove current cut from cutList, don't check it again
            if largestCustomerCut['orderline_no'] != x['orderline_no']:
                newCutList.append(x)
            else:
                # Set allocation to be false/un-satisfied
                setAllocationStatus(x, False, allocations)
        cutListDec = newCutList

print(allocations)
print(stockListDec)

整洁的打印输出:

[{'orderline_no': 4, 'size': Decimal('9'), 'allocation': False},
{'orderline_no': 3, 'size': Decimal('3'), 'allocation': 156712},
{'orderline_no': 2, 'size': Decimal('3.54999999999999982236431605997495353221893310546875'), 'allocation': 156711}, 
{'orderline_no': 1, 'size': Decimal('5.21999999999999975131004248396493494510650634765625'), 'allocation': 156710}]


[{'piece_no': 156710, 'size': Decimal('0.03000000000000024868995751604')}, 
{'piece_no': 156711, 'size': Decimal('0E-50')}, 
{'piece_no': 156712, 'size': Decimal('0.1000000000000000888178419700')}]

相关问题 更多 >