zip() : how about braid()

Huaiyu Zhu hzhu at localhost.localdomain
Fri Jul 21 03:21:07 EDT 2000


On Fri, 21 Jul 2000 15:25:28 +1200, Greg Ewing <see at my.signature> wrote:
>"Jürgen Hermann" wrote:
>> 
>> merge() is the best yet, since it's a common (programming) term with a
>> well-defined meaning.
>
>But it's a DIFFERENT meaning!
>
>merge([1,2,4,6,7,8],[3,5,9]) --> [1,2,3,4,5,6,7,8,9])
>

Complete solution! <0.9wink>

Following is a list of most of the operations discussed (except merge as
above), with one possible set of syntax - Or it might be just another can of
worms I'm opening.  :-)


a = [1, 2, 3];  b = [4, 5, 6]
#----------------------------------------------------
# Parallel loops:

c = tuples(a,b)             # c = [(1,4), (2,5), (3,6)]
c = lists(a,b)              # c = [[1,4], [2,5], [3,6]]
c = xtuples(a,b)            # c = ((1,4), (2,5), (3,6))
c = xlists(a,b)             # c = ([1,4], [2,5], [3,6])

#----------------------------------------------------
# prefix x has the same effect as in xrange in loops (delayed):

for pair in xtuples(a,b): 
    print pair              # print (1,4); print (2,5); print (3,6)
for pair in xlists(a,b):
    print pair              # print [1,4]; print [2,5]; print [3,6]

#----------------------------------------------------
# list comprehension - all elements that satisfy conditions

c = [x*2, y: x, y in xtuples(a,b)]          # c = [(2,4), (4,5), (6,6)]
c = [[x, y]: x, y in xtuples(a,b)]          # c = [[1,4], [2,5], [3,6]]
c = [x: x in a+b; x%2=0]                    # c = [2, 4, 6]
c = [x in a+b: x%2=0]                       # ditto
c = [x,y: x, y in xtuples(a,b); x>1; x+y<8] # c = [(2,5)]
c = [x,y in xtuples(a,b): x>1; x+y<8]       # ditto

#----------------------------------------------------
# Corresponding to old syntax

c = [x in a: f(x)]          # c = filter(f, x)
c = [f(x): x in a]          # c = map(f, a)

#----------------------------------------------------
# Iterated loops:

c = [x, y: x in a; y in b]  # c = [(1,4), (1,5), (1,6), (2,4), (2,5), ...]
c = [[x,y]: x in a; y in b] # c = [[1,4], [1,5], [1,6], [2,4], [2,5], ...]

for i, j in [i, j: i in a; j in b]:
    print i, j              # print 1,4; print 1,5; ...

for i in a; j in b:
    print i, j              # ditto

Huaiyu




More information about the Python-list mailing list