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

    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    List Matching, and outputting to text from multiple lists


    Say I have two lists like:

    Code:
    a = ['', '2', '3', '4', '']
    b = ['1', '5']
    List a has strings and has some items which are empty strings
    List b has some strings but no empty ones. The length of b is always less than a.

    How can I fill list a by checking every item in a to see if it is an empty string, taking the first item out of b (then removing it from b), and using this item in place of the empty string in a? so that I have a list like:

    Code:
    c = ['1', '2', '3', '4', '5']
    Also, say I have 3 lists, all of the same length. How can I print to a text file the first item in list 1, a space, the first item in list 2, another space and the first item in list 3. Then do the same for the next line, only this time use the second item and so on...? Eg

    Code:
    a = ['1', '2', '3', '4', '5']
    b = ['hi', 'are', 'you', 'ok', '?']
    c = ['yes', 'i', 'am', 'y', '?']
    and output to a txt file...
    Code:
    1 hi yes
    2 are i
    3 you am
    4 ok y
    5 ? ?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    The answer to your second question comes more easily:
    Code:
    >>> # you need zip
    >>> # succinctly:
    >>> print('\n'.join(' '.join(t)for t in zip(a,b,c)))
    1 hi yes
    2 are i
    3 you am
    4 ok y
    5 ? ?
    >>> # simplified
    >>> for t in zip(a,b,c):
    ...     print(' '.join(t))
    ... 
    1 hi yes
    2 are i
    3 you am
    4 ok y
    5 ? ?
    >>>
    Code:
    def jepot_substitute(a,b):
        result = a[:]
        j = 0
        for i in range(len(result)):
            if j == len(b):
                break
            if not result[i]:
                result[i] = b[j]
                j += 1
        return result
    
    print(jepot_substitute(['', '2', '3', '4', ''],['1', '5']))
    print(jepot_substitute(['', '2', '3', '4', ''],['short',]))
    print(jepot_substitute(['', '2', '3', '4', ''],['1','bark','unused']))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    434
    Rep Power
    67
    Or even:

    Code:
    from collections import deque
    def jepot_substitute(a, b):
        b = deque(b)
        return [s if s != '' else b.popleft() for s in a]


    (Sorry for the multiple edits of this post I guess this is now the definitive version...)
    Last edited by SuperOscar; July 15th, 2012 at 01:19 PM.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0
    Thanks guys, all your solutions worked and fixed both problems.

IMN logo majestic logo threadwatch logo seochat tools logo