En Fri, 30 Apr 2010 23:16:04 -0300, Jimbo <nill...@yahoo.com> escribió:

Hello I have a relatively simple thing to do; move an object from one
to list into another. But I think my solution maybe inefficient &
slow. Is there a faster better way to move my stock object from one
list to another? (IE, without having to use a dictionary instead of a
list or is that my only solution?)

[code]
class stock:

    code = "NULL"
    price = 0


stock_list1 = []
stock_list2 = []

def transfer_stock(stock_code, old_list, new_list):
    """ Transfer a stock from one list to another """
    # is there a more efficient & faster way to

    index = 0

    for stock in old_list:

        temp_stock = stock

        if temp_stock.code == stock_code:
            new_list.append(temp_stock)
            del old_list[index]
        index += 1

    return new_list[/code]

I'd do that in two steps:

def transfer_stock(stock_code, old_list, new_list):
  # find the indexes to transfer
  indexes = [i for i,stock in enumerate(old_list)
             if stock.code==stock_code]
  # actually transfer them
  for index in reversed(indexes):
    stock = old_list[index]
    new_list.append(stock)
    del old_list[index]
  # I would not return anything

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to