Thread: concat to list

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    4
    Rep Power
    0

    concat to list


    Hi All,

    Could someone please show me how to do this.

    I have a list of unknown length;

    ['item1', 'item4', 'item2', 'item1', 'item5', etc...]

    What I need to do is concat/append to each item in the list a value, based on itself and the previous item. I can do this with a load of if/else statements, but they would go on forever.

    So for example if previous item is item1a and if the current item is item4 it has a random choice of say abc, but if the previous item is say item1a and the current item is say item5 it has a new choice of say mno.

    I see it as sort of a multiplication lookup table, with current items on the x axis and previous items on the y. Is this called a 2d array?

    I'm new to python so I would really appreciate being shown how this is done.

    Thanks
  2. #2
  3. No Profile Picture
    Tattooed Python-Lovin' Freak-Boy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    orange county, CA
    Posts
    16
    Rep Power
    0
    not sure exactly what you're trying to do, could you give an example?
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Hope this example helps you understand how lists work in python.
    Code:
    #!/usr/bin/env python
    import random
    
    # First create an empty list
    list1 = []
    print "list1 is initially", list1
    
    # Now populate this list with other lists
    print "Populating list1 with stuff"
    nCount = 0
    for i in range(1,5):
            tmplist = []
            for j in range(1,random.randint(3, 8)):
                    nCount += 1
                    tmplist.append(nCount)
            list1.append(tmplist)
    
    # Now print the new list
    print "Now list1 is ", list1
    
    #Now go through the items of each list.
    print "\n\nPrinting the contents of list1 individually"
    for row in list1:
            print "Row is: ", row
            # Now print the column values
            for col in row:
                    print "\t", col
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    4
    Rep Power
    0
    Deleted
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    4
    Rep Power
    0
    Hi,

    Thanks for your replies.

    Code:
                     Previous Item in list
    
                [item1a, item1b,item1c,item2b, item2s, item2d]etc...
                item1   ('xyz') ('ghj')('mno') ('qwe')
    Current     item2   ('bsd') ('rty')('iop')
    item to     item3 etc...
    concat to 
    
    Real pseudocode follows:
    
    L1 = ['item2', 'item1', 'item2'] etc...
    
    for item in L1:
    append random.choice ('*') - 
    
    *letter to append comes from what the first item is.
    
    for next item in L1:
    append random.choice from where the columns and rows
    intersect.
    The above list might look like;

    ['item1a', 'item2s', 'item1w']

    I'm sorry for such a bad explanation, I hope you get the idea.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok, that was a better explination . But maybe you could give us an example of the output you'd like to get from the program.
    programming language development: www.netytan.com Hula

  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Give this a go, it might need a little editing to do exactly what you want but I think the basic idea is there.

    Code:
    #!/usr/bin/env python
    
    import random
    
    mapped = {'item1': 'abc', 'item2': 'def', 'item3': 'ghi'}
    
    passed = ['item1', 'item2', 'item3']
    results = []
    
    for index in range(len(passed)):
        letter = random.choice(mapped[passed[0]])
        results.append(passed[index] + letter)
    print results
    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    4
    Rep Power
    0
    Hi Mark,

    That's great. Thanks for wading through that 'real pseudocode'. It's definitely given me something I can work with.

    All the best,

    Calvin
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    No problem at all Calvin. Enjoy!
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo