append to items depending on prior item

M. Clift noone at here.com
Mon Oct 4 00:54:30 EDT 2004


Hi All,

First my appologies. I wrote this in my first post;

What I want to do is append to each item an extra value depending on the
previous item.

Too keen to get help, what I should have written is;

What I want to do is append to each item an extra value depending on the
previous item and itself.

Alex gave me this;

def weird_appender(sequence):
    choices = dict({None: 'abc'}, item1='abc', item2='def')
    previous = None
    for item in sequence:
        yield item+random.choice(choices.get(previous, choices[None]))
        previous = item
        print weird_appender(sequence)

What I want to do is append to an item, but rather than just getting this
value based on the previous choice
I'd like to say if previous item is 'item1a' and the current item is item2
append some choice. or else if the current item is
item3 append a diferent choice.

Now, I know none of you are on my payroll (and I have been asking for help a
lot), but if that is easy to impliment I would be grateful
if someone could show me how. However, if it isn't so easy to do, is the
previous code Alex gave me along the right lines for my new requirement?
So that if I go away and change / add to it myself I might eventually get it
to work. If not, could someone point me in the direction I should look
at other than a dictionary method. I see it as a sort of multiplication
lookup table i.e. previous items already appended are looked for along the
x axis, the current item unappended is on the y axis, reading along from
both gives the value to append to the current item.

Thanks for putting up with me,

Malcolm





More information about the Python-list mailing list