Append Problem

Steve Holden steve at holdenweb.com
Sat Jan 2 08:24:33 EST 2010


Victor Subervi wrote:
> Hi;
> I have the following code snippet:
> 
>   print 'Original: ', catChains, '<br />'
>   while i < MAXLEVEL:
>     flag = 0
>     j = 0
>     while j < len(parents):
>       for chain in catChains:
>         if parents[j] == chain[len(chain)-1]:
>           chain.append(children[j])
>           print '1: ', catChains, '<br />'
>           catChains.append(chain)
>           print '2: ', catChains, '<br />'
>           flag = 1
>       j += 1
>     i += 1
>     if flag == 0:
>       break
>   print 'Final: ', catChains
>  
> which prints this:
> 
> Original: [['prodCat1'], ['prodCat2']]
> 1: [['prodCat1', 'prodCat3'], ['prodCat2']]
> 2: [['prodCat1', 'prodCat3'], ['prodCat2'], ['prodCat1', 'prodCat3']]
> 1: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
> 'prodCat3', 'prodCat5']]
> 2: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
> 'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]
> Final: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
> 'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]
> 
> Why is it that my append statement *deletes* elements of my tuple? The
> entire code follows:
> 
There isn't a tuple is sight there - you are dealing with lists, and you
are modifying the very list you are iterating over.

Try rewriting the code to create a new list from the old one (i.e.
iterate over catChains and have your code append to an initially empty
list called, for example, newCatChains, then finally throw the old list
away with

  catChains = newCatChains

or some such).

[code snipped]
> 
> PS (Mainly, I believe, for Carsten): While programming is difficult for
> me, I am writing (and have pretty much finished) this (almost) fully
> automated shopping cart. I don't intend to write any other serious
> programming project. However, a shopping cart is vital to my Web design
> business. I will hereafter outsource my programming. But, as any good
> supervisor knows, one has to know how to supervise! If I can't read
> code, then I'm at the mercy of my programmer(s). If I can't afford to
> hire some top gun who can supervise, then it's my responsibility. Plus,
> if my programmer(s) quit, I need to step in and take over. I'm an ok
> businessman, and absolutely gifted in sales. And in poetry ;)
> beno
> 
Given that there are umpteen thousand hosting services who will let you
create a web site with shopping cart included I have to question whether
you are making the best use of your time here. Of course it's your time,
and your decision, so please don't think I am dictating to you.

While the learning you have performed in getting this system to work
(for some rather questionable value of "work", I can't help feeling, but
we'll overlook the horrors induced by your lack of programming
experience) is a testament to your persistence (and this list's
tolerance) I can't help feeling that it leaves you no better qualified
to supervise programmers.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list