zip() : how about braid()

Ben Wolfson rumjuggler at cryptarchy.org
Thu Jul 20 18:49:33 EDT 2000


On Thu, 20 Jul 2000 18:36:31 +0000, Peter Schneider-Kamp
<peter at schneider-kamp.de> wrote:

>Dennis Lee Bieber wrote:
>> 
>>         It's silly, but has anyone considered
>> 
>>         marry()
>> 
>> or
>> 
>>         partner()
>> 
>> as names for the function?
>
>It's not that silly. An older proposal favored marry().
>
>But imho marry() and partner() implicate the zipping
>of two lists (even stronger than the zipper zip()).
>
>OTOH somebody mentioned that it would be a great
>political statement. Marriage of an arbitrary number
>of arbitrary types (read: genders).

On the other hand, if it were marry(), then you could do all sorts of fun
stuff:

>>> def marry(*sequences):
	length = len(sequences[0])
	results = []
	for i in range(length):
		results.append([])
	for sequence in sequences:
		for i in range(length):
			results[i].append(sequence[i])
	return tuple(results)

>>> def divorce(sequence, num_results=2):
	results = []
	length = len(sequence)
	if length % num_results <> 0: raise Exception
	for i in range(num_results):
		results.append([])
	for i in range(length):
		results[i % num_results].append(sequence[i])
	return tuple(results)

>>> marry([1,3,5],[2,4,6])
([1, 2], [3, 4], [5, 6])
>>> divorce([1,2,3,4,5,6])
([1, 3, 5], [2, 4, 6])





More information about the Python-list mailing list