Cerberton
Cerberton

Reputation: 404

How to handle a KeyError when working with a list inside a list

I have written two functions in Python that I intend to re-use multiple times. Together they will allow me to calculate the total travel distance of a vehicle in a warehouse collecting goods from defined locations in a single aisle.

One function get_orderpick extracts two lists from input data in a dataFrame and returns them in a list, so the return clause looks like this: return [orderList, pickList].

When I run this function alone I appear to get a list with two lists stored inside it, no problem. BUT when I attempt to feed this through to my next function I get a KeyError.

So as I mentioned, the first function get_orderpick appears to work just fine, here's the full code:

def get_orderpick(df):
    # Produce a standard Python list of the picks
    # DataFrame -> numpy.array -> list

    pickList = df.sku.values.tolist()
    orderList = df.order.values.tolist()

    return [orderList, pickList]

Note - orderList is the sequence of picks, I need to track when the vehicle must return to base and start over again on the next order. It contains only numbers; pickList is the bay from which the pick must take place, this determines how far the vehicle must travel for each pick and contains a single capital letter in each entry.

Here is the distance calculation function:

def picking_distance(lists, layout):

    orderList = lists[1] #<------------------ issue here
    pickList = lists[2] 

    totalDistance = 0 # distance
    currentPos = 0  # position
    for i in range(len(pickList)):
        if orderList[i] == 1 and currentPos != 0:
            # new order, return to base
            totalDistance += currentPos
            currentPos = 0
            i -= 1 # begin the pick from base again
        else:
            nextPos = layout[pickList[i]]
            delta = abs(nextPos - currentPos)
            totalDistance += delta
            currentPos = nextPos
    return totalDistance

I would expect the code to produce the total distance traveled. But I get a KeyError when I try to separate out the orderList and pickList from the lists list. I am calling the functions together in the following way:

print(picking_distance(layout, get_orderpick(data)))

Thanks for your help!

P.S. I pass through a dictionary called layout on the second function. This determines the distance between picking locations.

Upvotes: 2

Views: 1436

Answers (1)

Cerberton
Cerberton

Reputation: 404

So...I made a mistake and switched my arguments around when calling my own function.

This means I was passing a dictionary where the function expected a list of lists.

Switching the arguments has solved the problem and it works beautifully.

print(picking_distance(get_orderpick(data), layout))

Moral of the story

If you're getting a dictionary error when you expect to be handling lists...you're probably passing your arguments in the wrong order.

Thanks for your time! :D

Upvotes: 2

Related Questions