[Tutor] Re: Unzip

Mike Serpa onav@yahoo.com
Sun, 15 Jul 2001 02:06:52 -0700 (PDT)


How serendipitous.  This is just what I was looking for.

I was trying to figure out how to print some lists of unequal length in
columns and came up with:

--------------
def printListColumns(*lists):
    paddedLists = padLists(lists)
    for row in range(len(paddedLists[0])):  # number of rows
        for column in paddedLists:          # number of columns
            print str(column[row]).ljust(20),
        print 

def padLists(lists):
    longestLength = findLongest(lists)
    for list in lists:
        while len(list) < longestLength:   # pad shorter lists with ''
            list.append('')
    return lists

def findLongest(lists):                    # find length of longest
list
    longestLength = 0
    for list in lists:
        if len(list) > longestLength:
            longestLength = len(list)
    return longestLength

a = [1,2,3]
b =[4,5,6]
c = [7,8,9,10]

printListColumns(a,b,c)
------------------------

Which I've now shortened to:

-------------------

def printListColumns(*lists):
    import lace
    lacedList = apply(lace.lace,lists)      # sort into columns
    lacedList = map(list,lacedList)         # make mutable 
    paddedList = map(padList,lacedList)     # change all None's to ''
    for row in paddedList:
        for column in row: 
            print str(column).ljust(20),
        print

def padList(list):                      # padded = nullstrings
    for item in list:
        if item == None:
            list[list.index(None)] = ''
    return list

a = [1,2,3]
b = [4,5,6]
c = [7,8,9,10]

printListColumns(a,b,c)

-------------------

Hmmmmm, not too much reduction,  but I'm sure there is a better way to
replace the None fields with nullstrings,  but I'm a tired newbie who
needs to go to bed.



> Message: 3
> To: tutor@python.org
> Date: Sat, 14 Jul 2001 18:14:55 +0900
> From: "kevin parks" <kp87@lycos.com>
> Reply-To: kp87@lycos.com
> Subject: [Tutor] Unzip
> Organization: Lycos Mail  (http://mail.lycos.com:80)
> 
> Wow, i had no idea that there was a built-in called zip. I rolled my
> own, but called it lace (like a shoe) and even have an "unlace", but
> i think, inferior to your Unzip, which also does x,z=unzip(l,0,2).:
> 
> def lace(*lists):
> 	"""lace: forms a list by interlacing elements from several lists.
> 	
> 	e.g.: 
> 	a=[1,2,3,4]; b=[100,101,102,103]; c=['w','x','y','z']	
> 	d=lace(a,b,c); print d
> 	
> 	yields a single list of nested tuples:
> 	[(1, 100, 'w'), (2, 101, 'x'), (3, 102, 'y'), (4, 103, 'z')]
> 	
> 	**NOTE: Short lists are extended with values of None to match the
> 	longest list if necessary.
> 	"""
> 
> 	# 99.999% of the work is done courtesy of Python's groovey built-in 
> 	# map() function.
> 	# t = map(function, list1, list2, ...) 
> 	# None as function means do nothing to list elements!
> 
> 	return apply(map,(None,)+lists)
> 
> 
> So the built-in just does the exact same thing acording to the docs:"
>        zip() is similar to map() with an initial argument of None."
> 
> look at that! Only difference zip will truncate to the shortest list.
> Using map the above pads the short list with None, which i think is
> better. 


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/