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

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Question Print elements of an array


    I am beginner with python
    If I have an array or list of lisits
    how can I print the values of the items that represent the "Z" letter
    e.g for the list :
    list1 = [ [ 1,2,3],[4,5,6],[7,8,9] ]
    it will print 1,2,3,5,7,8,9
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    509
    Rep Power
    33
    Originally Posted by The Ghost
    how can I print the values of the items that represent the "Z" letter
    I'm not sure what that means.
    e.g for the list :
    list1 = [ [ 1,2,3],[4,5,6],[7,8,9] ]
    it will print 1,2,3,5,7,8,9
    You first flatten the list which can be done with
    Code:
    """------------------------------
     itertools chain
    ---------------------------------
    """
    x = [['foo'], ['bar', 'baz'], ['quux'], ("tup_1", "tup_2"), {1:"one", 2:"two"}]
    print list(itertools.chain(*x))
    
    """------------------------------
    or recursion
    -----------------------------
    """
    def flatten(test):
       for el in test:
          if type(el) is list:
             for sub in flatten(el):
                yield sub
          else:
             yield el
    
    print list(flatten([ [ 1,2,3],[4,5,6],[7,8,9] ])) 
    
    """------------------------------------------------------------------
    or if you know it will always be a list of lists one level deep
    ---------------------------------------------------------------------
    """
    result = []
    x = [ [ 1,2,3],[4,5,6],[7,8,9] ]
    for sub in x:
        result.extend(sub)
    print result
    Last edited by dwblas; June 17th, 2013 at 04:51 PM.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    Or you can do this ...
    Code:
    mylist = [[1,2,3],[4,5,6],[7,8,9]]
    s = str(mylist)
    s = s.replace('[', '').replace(']', '')
    print(s)
    
    ''' result ...
    1, 2, 3, 4, 5, 6, 7, 8, 9
    '''
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Took me a while, but I think I now know what the original poster was asking. Consider the following matrix
    Code:
    1  2  3
    4  5  6
    7  8  9
    Now imagine someone drew the letter 'Z' in the matrix. Basically, the code should print out those digits that would form the shape of the letter Z (i.e.)
    Code:
    1  2  3
       5   
    7  8  9
    or rather "1,2,3,5,7,8,9".

    Let me know if my interpretation of the request is correct.
    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

IMN logo majestic logo threadwatch logo seochat tools logo