Title: Cartesian Product
Slug: cartesian_product
Summary: Cartesian Product
Date: 2016-05-01 12:00
Category: Python
Tags: Basics
Authors: Chris Albon
In [1]:
# import pandas as pd
import pandas as pd
In [2]:
# Create two lists
i = [1,2,3,4,5]
j = [1,2,3,4,5]
In [3]:
# List every single x in i with every single y (i.e. Cartesian product)
[(x, y) for x in i for y in j]
Out[3]:
In [4]:
# An alternative way to do the cartesian product
# import itertools
import itertools
# for two sets, find the the cartisan product
for i in itertools.product([1,2,3,4,5], [1,2,3,4,5]):
# and print it
print(i)