You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修正生成{0,1}与二元组配对全组合的代码错误?

How to Generate All Possible Pairings Between {0,1} and the 2D Binary Tuples

Hey there! Let's fix this up and get you the exact 16-element list you're looking for. First, let's clarify the goal: we need every possible way to assign a 0 or 1 to each of the four 2D tuples (0,0), (0,1), (1,0), (1,1). Each assignment will be a list of 4 ((x,y), z) entries, and since there are 2 choices per tuple, we get 2⁴=16 total combinations.

Why Your Previous Approaches Didn't Work

  • numpy.meshgrid mistake: You tried combining the tuple set with {0,1} directly, but meshgrid wasn't structured to map each tuple to a unique 0/1 value across all combinations. The output you got was a messy multi-dimensional array instead of the paired assignments you needed.
  • itertools.combinations is the wrong tool: This function generates subsets of elements (e.g., pairs of tuples), but we don't want subsets—we want to assign a 0/1 to every tuple in the set, covering all possible assignment permutations.

The Correct Solution with itertools.product

itertools.product is perfect here because it generates the Cartesian product of sets, which is exactly what we need for all 0/1 assignment combinations. Here's the step-by-step code:

import itertools

# Step 1: Create the set of 2D binary tuples
bset = [0, 1]
fset = list(itertools.product(bset, bset))  # Gives [(0,0), (0,1), (1,0), (1,1)]

# Step 2: Generate all possible 0/1 sequences for 4 elements (one per tuple)
# `repeat=4` means we're creating sequences of length 4, each element 0 or 1
z_assignments = itertools.product(bset, repeat=4)

# Step 3: Pair each tuple with its corresponding 0/1 value in each sequence
final_result = [list(zip(fset, z_seq)) for z_seq in z_assignments]

Verify the Result

  • len(final_result) will be 16 (exactly the number of combinations we need)
  • Each inner list has 4 entries, e.g.:
    • First entry: [((0, 0), 0), ((0, 1), 0), ((1, 0), 0), ((1, 1), 0)] (all zeros assigned)
    • Middle entry example: [((0, 0), 0), ((0, 1), 1), ((1, 0), 0), ((1, 1), 1)]
    • Last entry: [((0, 0), 1), ((0, 1), 1), ((1, 0), 1), ((1, 1), 1)] (all ones assigned)

Alternative NumPy Approach

If you prefer using NumPy, you can adjust your meshgrid usage to generate the 0/1 sequences correctly:

import numpy as np

bset = np.array([0, 1])
# Create the 2D tuple set (same as before)
fset = np.array(np.meshgrid(bset, bset)).T.reshape(-1, 2)
fset = [tuple(i) for i in fset]

# Generate all 4-length 0/1 sequences using meshgrid
z_assignments = np.array(np.meshgrid(bset, bset, bset, bset)).T.reshape(-1, 4)

# Pair tuples with assignments
final_result = [list(zip(fset, z_seq)) for z_seq in z_assignments]

This will give you the exact same 16-element list as the itertools version.

内容的提问来源于stack exchange,提问作者user11562913

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:48:41