[Tutor] Unzip

kevin parks kp87@lycos.com
Sat, 14 Jul 2001 18:14:55 +0900


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. 

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> c=[7,8,9]
>>> d = [7,8,9,0]
>>> l=zip(a,b,c)
>>> l
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> l2=zip(a,b,d)
>>> l2
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> l3=lace(a,b,d)
>>> l3
[(1, 4, 7), (2, 5, 8), (3, 6, 9), (None, None, 0)]

As for Unzip, Christopher, thanks! your "useless" little piece of code is not so useless to me. in fact i love to mess with lists and often need to. I am building up quite a collection of them. Unzip is wonderful. Let me know if you have any others. Particularly ones that reorder or make patterns, or do stochastics. I collect and make them including silly ones (that i actually sometimes even use), like:

def mirror(seq):		# odd symmetry mirroring
	foo=seq[:-1]		# copy list, excluding last element
	foo.reverse()		# flip it
	seq.extend(foo)		# stick it on the end of the other list
	return seq


and:


def scramble(foo):
	inList = foo[:]
	outList = []
	#print inList, "\n", outList,"\n"	# while there are still elements in the list do:
        while inList:        			# -- (will stop looping when seq is empty) --
                element = random.choice(inList)	# choose one element at random
		#print "--",element, "--"
		outList.append(element)     	# stick it on the end of the output list
                inList.remove(element)		# remove pick from original seq
	return outList
	


>Message: 11
>Date: Fri, 13 Jul 2001 23:01:36 -0500
>To: tutor@python.org
>Cc: rob@jam.rr.com
>From: "Christopher Smith" <csmith@blakeschool.org>
>Subject: [Tutor] Unzip
>
>Here's a piece of useless code which extracts lists that have been zipped 
>together (or mapped together with None: 
>	map(Nane,l1,l2) is the same as zip(l1,l2)
>
>Does anyone use something else to do this?
>
>---code start---
>def unzip(l,*jj):
>	'''Return all (if none are specified) or some 
>	   elements in the tuples of l that were zipped together.
>	'''
>	#
>	# Christopher P. Smith 7/13/2001
>	#
>	if jj==():
>		jj=range(len(l[0]))
>	rl = [[li[j] for li in l] for j in jj] # a list of lists
>	if len(rl)==1:
>		rl=rl[0] #convert list of 1 list to a list
>	return rl
>
>a=[1,2,3]
>b=[4,5,6]
>c=[7,8,9]
>l=zip(a,b,c)
>
>x,y,z= unzip(l) # get them all: x=[1,2,3], y=[4,5,6], z=[7,8,9]
>y=unzip(l,1)    # get the 1th one: y as above
>x,z=unzip(l,0,2)# get the 0th and 2th ones: x and z as above
>
>---code end---
>
>/c

>


Get 250 color business cards for FREE!
http://businesscards.lycos.com/vp/fastpath/