如何将含4个变量的图生成代码泛化为支持更多变量a[i]
Great question! Let's break down how to generalize this code so it scales easily to more variables instead of being hardcoded for 4. The core idea is to replace all repeated, hardcoded logic (like checking modulo 4, referencing a1-a4, and repeating loops) with dynamic logic driven by a variable count parameter or a configuration map that defines your variable names and their associated indices.
First, define a base configuration to make your code flexible. Adjust these values to match your actual variable naming convention or desired scale:
# Configure your variables here - tweak these to add/remove variables num_vars = 4 # Change to 5, 8, etc. to scale up var_map = {0: 'a1', 1: 'a2', 2: 'a3', 3: 'a4'} # Extend this map for new variables
Now let's rewrite each block with this generalized approach:
Block 1: Dynamic String Construction
Instead of nested if-else checks for modulo 4, use the var_map to look up the correct variable name based on the index modulo num_vars:
a = ''.join(map(str, [var_map.get(x % num_vars, f'a{num_vars}') for x in comb_3bit[i]]))
Block 2: Populating all and good Lists
Replace the 4 separate loops with a single loop over your variable indices/names. We'll dynamically get the count for each variable and calculate k using the index:
good = [] all_list = [] # Renamed from "all" since it's a reserved Python keyword for idx, var_name in var_map.items(): # Get the count of the current variable in your target list count = kombinacje_3bit_bez_powtorzen[0].count(var_name) for j in range(count): k = j * num_vars + idx all_list.append(k) good.append(k)
Block 3: Ensuring All Variable Indices Are Present in good2
Loop through all possible indices (0 to num_vars-1) instead of hardcoding 0-3. Check if any element in good2 matches the modulo condition, and insert the index if missing:
for idx in range(num_vars): if not any(i % num_vars == idx for i in good2): good2.insert(idx, idx)
Block 4: Extracting Last Elements for Each Variable Group
Use a dictionary to store your p groups instead of separate variables (p0-p3). This makes it easy to access the last element for any variable index:
p_groups = {} z_values = {} for idx in range(num_vars): p_groups[idx] = [i for i in good2 if i % num_vars == idx] z_values[idx] = p_groups[idx][-1] # Grab the last element of each group
Block 5: Adding Nodes to the Graph
Loop through all variable indices to add nodes dynamically, instead of hardcoding 4 node additions:
for idx in range(num_vars): node = z_values[idx] + h * num_vars g.add_node(node) good2.append(node)
Block 6: Building the sources Dictionary
Use a dictionary comprehension to generate the sources map dynamically, based on your variable configuration:
sources = { var_name: [i for i in good2 if i % num_vars == idx] for idx, var_name in var_map.items() }
Key Benefits of This Approach
- Scalability: Just update
num_varsandvar_mapto add more variables (e.g., setnum_vars=6and extendvar_mapto includea5,a6). - Maintainability: No more copying/pasting code blocks for each new variable—changes only need to be made in one place.
- Readability: The dynamic logic makes it clearer that all variables follow the same pattern, instead of hiding that pattern in repeated code.
内容的提问来源于stack exchange,提问作者Tomasz Przemski

