|
| 1 | +""" |
| 2 | +cartesian_product.py |
| 3 | +
|
| 4 | +Demonstrates how to use Python list comprehensions to find the cartesian product |
| 5 | +of two lists. |
| 6 | +
|
| 7 | +The cartesian product of two lists A and B is the set of all ordered pairs (a, b) |
| 8 | +where a is from A and b is from B. |
| 9 | +""" |
| 10 | + |
| 11 | +# ============================================================================ |
| 12 | +# BASIC CARTESIAN PRODUCT WITH LIST COMPREHENSION |
| 13 | +# ============================================================================ |
| 14 | + |
| 15 | +print("=" * 60) |
| 16 | +print("CARTESIAN PRODUCT WITH LIST COMPREHENSION") |
| 17 | +print("=" * 60) |
| 18 | + |
| 19 | +# Example 1: Simple cartesian product |
| 20 | +list_a = [1, 2, 3] |
| 21 | +list_b = ['a', 'b'] |
| 22 | + |
| 23 | +# Traditional way with nested for loops: |
| 24 | +cartesian_traditional = [] |
| 25 | +for a in list_a: |
| 26 | + for b in list_b: |
| 27 | + cartesian_traditional.append((a, b)) |
| 28 | + |
| 29 | +print(f"\nList A: {list_a}") |
| 30 | +print(f"List B: {list_b}") |
| 31 | +print(f"\nTraditional way (nested loops):") |
| 32 | +print(f" {cartesian_traditional}") |
| 33 | + |
| 34 | +# List comprehension way (much more concise!): |
| 35 | +cartesian_comprehension = [(a, b) for a in list_a for b in list_b] |
| 36 | +print(f"\nList comprehension:") |
| 37 | +print(f" {cartesian_comprehension}") |
| 38 | + |
| 39 | +# Syntax: [(a, b) for a in list_a for b in list_b] |
| 40 | +# Read as: "for each a in list_a, for each b in list_b, create tuple (a, b)" |
| 41 | + |
| 42 | + |
| 43 | +# ============================================================================ |
| 44 | +# MORE EXAMPLES |
| 45 | +# ============================================================================ |
| 46 | + |
| 47 | +print("\n" + "=" * 60) |
| 48 | +print("MORE EXAMPLES") |
| 49 | +print("=" * 60) |
| 50 | + |
| 51 | +# Example 2: Cartesian product of numbers |
| 52 | +numbers = [1, 2, 3] |
| 53 | +colors = ['red', 'blue'] |
| 54 | +combinations = [(num, color) for num in numbers for color in colors] |
| 55 | +print(f"\nNumbers: {numbers}") |
| 56 | +print(f"Colors: {colors}") |
| 57 | +print(f"All combinations: {combinations}") |
| 58 | + |
| 59 | +# Example 3: Cartesian product with strings |
| 60 | +suits = ['♠', '♥', '♦', '♣'] |
| 61 | +ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] |
| 62 | +cards = [(rank, suit) for suit in suits for rank in ranks] |
| 63 | +print(f"\nSuits: {suits}") |
| 64 | +print(f"Ranks: {ranks}") |
| 65 | +print(f"Total cards: {len(cards)}") |
| 66 | +print(f"First 5 cards: {cards[:5]}") |
| 67 | +print(f"Last 5 cards: {cards[-5:]}") |
| 68 | + |
| 69 | +# Example 4: Cartesian product with filtering |
| 70 | +# Only include pairs where the number is greater than the index |
| 71 | +list_x = [1, 2, 3, 4] |
| 72 | +list_y = [0, 1, 2] |
| 73 | +filtered_pairs = [(x, y) for x in list_x for y in list_y if x > y] |
| 74 | +print(f"\nList X: {list_x}") |
| 75 | +print(f"List Y: {list_y}") |
| 76 | +print(f"Pairs where x > y: {filtered_pairs}") |
| 77 | + |
| 78 | +# Example 5: Cartesian product with transformation |
| 79 | +# Create pairs and calculate their product |
| 80 | +nums1 = [2, 3, 4] |
| 81 | +nums2 = [5, 6] |
| 82 | +products = [(a, b, a * b) for a in nums1 for b in nums2] |
| 83 | +print(f"\nNumbers 1: {nums1}") |
| 84 | +print(f"Numbers 2: {nums2}") |
| 85 | +print(f"Pairs with products: {products}") |
| 86 | + |
| 87 | + |
| 88 | +# ============================================================================ |
| 89 | +# COMPARISON: NESTED LOOPS vs LIST COMPREHENSION |
| 90 | +# ============================================================================ |
| 91 | + |
| 92 | +print("\n" + "=" * 60) |
| 93 | +print("COMPARISON: NESTED LOOPS vs LIST COMPREHENSION") |
| 94 | +print("=" * 60) |
| 95 | + |
| 96 | +# Task: Create all pairs from two lists |
| 97 | + |
| 98 | +list1 = [10, 20] |
| 99 | +list2 = ['x', 'y', 'z'] |
| 100 | + |
| 101 | +# Method 1: Traditional nested for loops |
| 102 | +result1 = [] |
| 103 | +for item1 in list1: |
| 104 | + for item2 in list2: |
| 105 | + result1.append((item1, item2)) |
| 106 | +print(f"\nNested loops: {result1}") |
| 107 | + |
| 108 | +# Method 2: List comprehension (more Pythonic!) |
| 109 | +result2 = [(item1, item2) for item1 in list1 for item2 in list2] |
| 110 | +print(f"List comprehension: {result2}") |
| 111 | + |
| 112 | +# Both produce the same result, but list comprehension is: |
| 113 | +# - More concise (one line vs multiple lines) |
| 114 | +# - More readable (once you understand the syntax) |
| 115 | +# - More Pythonic (follows Python best practices) |
| 116 | + |
| 117 | + |
| 118 | +# ============================================================================ |
| 119 | +# CARTESIAN PRODUCT OF MORE THAN TWO LISTS |
| 120 | +# ============================================================================ |
| 121 | + |
| 122 | +print("\n" + "=" * 60) |
| 123 | +print("CARTESIAN PRODUCT OF MORE THAN TWO LISTS") |
| 124 | +print("=" * 60) |
| 125 | + |
| 126 | +# You can extend this pattern to more than two lists |
| 127 | +list_a = [1, 2] |
| 128 | +list_b = ['a', 'b'] |
| 129 | +list_c = ['x', 'y'] |
| 130 | + |
| 131 | +# Three-way cartesian product |
| 132 | +triples = [(a, b, c) for a in list_a for b in list_b for c in list_c] |
| 133 | +print(f"\nList A: {list_a}") |
| 134 | +print(f"List B: {list_b}") |
| 135 | +print(f"List C: {list_c}") |
| 136 | +print(f"All triples: {triples}") |
| 137 | +print(f"Total combinations: {len(triples)}") |
| 138 | + |
| 139 | + |
| 140 | +# ============================================================================ |
| 141 | +# SUMMARY |
| 142 | +# ============================================================================ |
| 143 | + |
| 144 | +print("\n" + "=" * 60) |
| 145 | +print("SUMMARY") |
| 146 | +print("=" * 60) |
| 147 | +print(""" |
| 148 | +Cartesian Product with List Comprehension: |
| 149 | + [(a, b) for a in list_a for b in list_b] |
| 150 | +
|
| 151 | +Key Points: |
| 152 | + 1. The cartesian product creates all possible pairs from two lists |
| 153 | + 2. List comprehensions make this concise and readable |
| 154 | + 3. Read nested comprehensions from left to right: |
| 155 | + - "for each a in list_a, for each b in list_b, create (a, b)" |
| 156 | + 4. You can add conditions: [(a, b) for a in A for b in B if condition] |
| 157 | + 5. You can extend to more lists: [(a, b, c) for a in A for b in B for c in C] |
| 158 | +
|
| 159 | +Note: For very large lists, consider using itertools.product() which is |
| 160 | +more memory-efficient as it returns an iterator. |
| 161 | +""") |
0 commit comments