first - maybe sombody can help me with the english expression for the
german word 'kreuzprodukt' - this my question is dealing with
-----------------------------------------------
example -
list1 = [1, 2]
list2 = [a, b, c]
[(x,y) for x in list1 for y in list2]
the result is the "kreuzprodukt"
[(1,a), (1,b), (1,c), (2,a), (2,b), (2,c)]
-----------------------------------------------
question -
i need to keep the number of lists variable
e.g. the next case should handle three lists
[(a1, a2, a3) for a1 in list1 for a2 in list2 for a3 in list3]
i cannot put variables into this algorythm or they don't do what i expect
maybe there is a simple solution, but i cannot find it
<thx>
fritz
(-:fs)
try: http://dict.tu-chemnitz.de/
"cross product"
> -----------------------------------------------
>
> example -
>
> list1 = [1, 2]
> list2 = [a, b, c]
>
> [(x,y) for x in list1 for y in list2]
>
> the result is the "kreuzprodukt"
> [(1,a), (1,b), (1,c), (2,a), (2,b), (2,c)]
>
> -----------------------------------------------
>
> question -
>
> i need to keep the number of lists variable
>
> e.g. the next case should handle three lists
>
> [(a1, a2, a3) for a1 in list1 for a2 in list2 for a3 in list3]
>
> i cannot put variables into this algorythm or they don't do what i expect
> maybe there is a simple solution, but i cannot find it
you're sure you want to program this yourself? there is numpy, i think it
can handle that.
anyway, this one is taking list of list (or tuples):
>>> xp=lambda l1,l2:[x+y for x in l1 for y in l2]
>>> xp([(1, 'a'), (2, 'b')], [(1, 'a'), (2, 'b')])
[(1, 'a', 1, 'a'), (1, 'a', 2, 'b'), (2, 'b', 1, 'a'), (2, 'b', 2, 'b')]
zip can be used to transform a list to a list of list (trasposed)
that way you can use it nested:
>>> xp(xp(zip([1,2]), zip([a,b,c])), zip([3,4]))
chris
--
Chris <clie...@gmx.net>
"cross-product" or "cartesian product", I think.
In Python 2.2, the following code seems to work:
def cross(l=None, *args):
if l is None:
# The product of no lists is 1 element long,
# it contains an empty list
yield []
return
# Otherwise, the product is made up of each
# element in the first list concatenated with each of the
# products of the remaining items of the list
for i in l:
for j in cross(*args):
yield [i] + j
for i in cross("12", "abc", "xyzw"): print i
before generators, you'd have to write the code in a slightly different
way...
Jeff
print CartesianProduct([1,2], list('abc'), 'do re mi'.split())
Raymond Hettinger
"steindl fritz" <pyt...@floSoft.org> wrote in message
news:10226190...@newsmaster-04.atnet.at...
Cross product.
> i cannot put variables into this algorythm or they don't do what i expect
> maybe there is a simple solution, but i cannot find it
I don't see an obvious way to do it with list comprehensions. The
obvious way to do it is with a recursive function.
>>> list1 = [1,2]
>>> list2 = ['a','b','b']
>>> list3 = [1,2,3,4]
def crossProduct(*listOfLists):
... first = listOfLists[0]
... retVal = []
... for item in first:
... retVal.append( [item] )
... i = 1
... while i < len(listOfLists):
... next = listOfLists[i]
... retVal = [x[:]+[y] for x in retVal for y in next]
... i += 1
... return retVal
...
>>> crossProduct(list1)
[[1], [2]]
>>> crossProduct(list1,list2)
[[1, 'a'], [1, 'b'], [1, 'b'], [2, 'a'], [2, 'b'], [2, 'b']]
>>> crossProduct(list1,list2,list3)
[[1, 'a', 1], [1, 'a', 2], [1, 'a', 3], [1, 'a', 4],
[1, 'b', 1], [1, 'b', 2], [1, 'b', 3], [1, 'b', 4],
[1, 'b', 1], [1, 'b', 2], [1, 'b', 3], [1, 'b', 4],
[2, 'a', 1], [2, 'a', 2], [2, 'a', 3], [2, 'a', 4],
[2, 'b', 1], [2, 'b', 2], [2, 'b', 3], [2, 'b', 4],
[2, 'b', 1], [2, 'b', 2], [2, 'b', 3], [2, 'b', 4]]
--
-
"steindl fritz" <pyt...@floSoft.org> wrote in message
news:10226190...@newsmaster-04.atnet.at...
hello and many thx to all,
----------------------------------------------
my shortest version was only like this )-:
def cross(listOfLists):
if len(listOfLists) == 1:
grid = []
for i in range(len(listOfLists[0])):
grid.append([listOfLists[0][i]])
return grid
else:
list = listOfLists.pop(0)
grid = cross(listOfLists)
lengthList = len(list)
lengthGrid = len(grid)
for i in range(lengthList-1):
for j in range(lengthGrid):
newList = []
for i in range(len(grid[j])): newList.append(grid[j][i])
grid.append(newList)
for i in range(lengthList):
for j in range(lengthGrid):
grid[i*lengthGrid + j].append(list[i])
return grid
<btw> generators will not be working with my
ZOPE 2.5 installation (python 2.1.2)
------------------------------------------------
the shortest version was like this -
def cross(x):
if x == []: return [[]]
return [[z]+y for y in cross(x[1:]) for z in x[0]]
Usage: cross([list1,list2]), cross([list1,list2,list3]), ...
special thx to Paul
--------------------------------------------------
steindl fritz
(-:fs)
vienna, earth, universe
On Wed, 29 May 2002, steindl fritz wrote:
>
> the shortest version was like this -
>
>
> def cross(x):
> if x == []: return [[]]
> return [[z]+y for y in cross(x[1:]) for z in x[0]]
>
> Usage: cross([list1,list2]), cross([list1,list2,list3]), ...
>
How about:
reduce( lambda X1,X2: [ one+[two] for one in X1 for two in X2],
listoflists, [[]] )
... or if you want a list of tuples instead:
reduce( lambda X1,X2: [ one+(two,) for one in X1 for two in X2],
listoflists, [()] )
>>> a
[1, 2]
>>> b
['a', 'b', 'c']
>>> c
[100, 200]
>>> z
['x', 'y', 'z']
>>> reduce( lambda X1,X2: [ one+[two] for one in X1 for two in X2], [a,b,c,z], [[]] )
[[1, 'a', 100, 'x'], [1, 'a', 100, 'y'], [1, 'a', 100, 'z'], [1, 'a', 200,
'x'], [1, 'a', 200, 'y'], [1, 'a', 200, 'z'], [1, 'b', 100, 'x'], [1, 'b',
100, 'y'], [1, 'b', 100, 'z'], [1, 'b', 200, 'x'], [1, 'b', 200, 'y'], [1,
'b', 200, 'z'], [1, 'c', 100, 'x'], [1, 'c', 100, 'y'], [1, 'c', 100,
'z'], [1, 'c', 200, 'x'], [1, 'c', 200, 'y'], [1, 'c', 200, 'z'], [2, 'a',
100, 'x'], [2, 'a', 100, 'y'], [2, 'a', 100, 'z'], [2, 'a', 200, 'x'], [2,
'a', 200, 'y'], [2, 'a', 200, 'z'], [2, 'b', 100, 'x'], [2, 'b', 100,
'y'], [2, 'b', 100, 'z'], [2, 'b', 200, 'x'], [2, 'b', 200, 'y'], [2, 'b',
200, 'z'], [2, 'c', 100, 'x'], [2, 'c', 100, 'y'], [2, 'c', 100, 'z'], [2,
'c', 200, 'x'], [2, 'c', 200, 'y'], [2, 'c', 200, 'z']]
>>>
-- Steve Majewski
In many applications of vectors to problems in geometry, physics, and engineering it is of interest to construct a vecotr in 3-space that is perpendicular to two given vectors. In this section we introduce a type of vector multiplication the facilitates this construction
DEFINITION: If u=(u1,u2,u3) and v=(v1,v2,v3) are vectors in 3-space, then the cross product
is the vector defined by
u x v = (u2v3 - u3v2 , u3v1 - u1v3 , u1v2 - u2v1)
John
Cartesian products are often called cross products.
And if i,j,k are the unit vectors, you can also write this
as the determinant of the matrix
i j k
u1 u2 u3
v1 v2 v3
(which is easier to remember for me).
The direction of the result is perpendicular to the plane that
includes u and v, in the direction a right-hand screw would move
if turned as u rotates into v (assuming a right hand coordinate
system). Easy to see with pure x and y axis vectors:
i j k
u1 0 0
0 v2 0
=> k*(u1*v2) (+ in the z direction of unit vector k)
vs the same vectors but turning the other way (v x u):
i j k
0 v2 0
u1 0 0
=> -k*(v2*u1) (also along z axis but opposite direction)
thought I'd mention, in case someone hadn't seen this before ;-)
Regards,
Bengt Richter