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

获取组合列表的序号:将带前缀元素组合转为纯序号输出

Solution to Get Numeric Combinations

Hey there! Let's sort this out for you. The problem right now is that your code is using the full strings from my_list (like "a1") when generating combinations, but you only want the numeric part of each element. Here are two simple ways to get your desired output:

Method 1: Preprocess the List First

First, we'll extract the numeric part from each element in your original list, then generate combinations from this new numeric list:

import itertools
my_list = ["a1","a2","a3","a4"]
# Extract the numeric substring from each item (removes the leading "a")
numeric_list = [item[1:] for item in my_list]
# Generate combinations using the numeric list
for combo in itertools.combinations(numeric_list, 2):
    print(combo, end=" ")

Method 2: Process Combos On-the-Fly

If you prefer not to create a separate list, you can extract the numeric parts directly when iterating over the combinations:

import itertools
my_list = ["a1","a2","a3","a4"]
for original_combo in itertools.combinations(my_list, 2):
    # Extract numbers from each element in the combo
    numeric_combo = (original_combo[0][1:], original_combo[1][1:])
    print(numeric_combo, end=" ")

Both methods will produce your expected output:

('1', '2') ('1', '3') ('1', '4') ('2', '3') ('2', '4') ('3', '4')

内容的提问来源于stack exchange,提问作者Hüseyin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:45:20