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

Python比较/计算小数列表

Python比较/计算小数列表,python,decimal,Python,Decimal,我正在使用python编写一个API,该API接收小数列表,例如: cutList = [3.00, 5.20, 2.50] 现在这些小数是切割尺寸,我的API需要测试这些切割尺寸是否有库存。因此,我的API查询所有当前库存大小,并返回如下内容: stockList = [9.00, 3.00] 我现在需要做的是评估这两个列表,看看cutList和stockList中的所有值是否都来自stockList 我不能把所有的列表都加在一起,因为这样计算不正确,例如(下面的计算是正确的,但是一个8.

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

cutList = [3.00, 5.20, 2.50]
现在这些小数是切割尺寸,我的API需要测试这些切割尺寸是否有库存。因此,我的API查询所有当前库存大小,并返回如下内容:

stockList = [9.00, 3.00]
我现在需要做的是评估这两个列表,看看cutList和stockList中的所有值是否都来自stockList

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

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

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


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

长话短说,我决定推出自己的,有一个我可以使用的numPy库组合(主要是numPy where),但就我列表的大小而言,这是合适的

注意,我决定扩展我的演示数据,以包括所需切割的订单行编号,以及我的库存清单,以返回件号(库存编号),这样,当找到匹配项时,我可以设置一个单独的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')}]

我认为这是一个如此(本质上)的问题,您的问题是找到
stockList
中大于或等于
cutList
中最大元素的最小元素,从
cutList
中减去最大元素,然后重复?@Linuxios有效地执行。@crooksey:您当然可以尝试用这种方式实现它。它可能不是最有效的,但我也是如此只要列表很小,我就不会担心了。@Linuxios是的,这是我的计划,如果没有其他图书馆已经这样做了,cutList将不会有超过10个条目,stock list不可能有超过5个条目。
[{'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')}]