英文:
Remove parenthesis from itertools.combinations list
问题
我想制作一个在一定范围内的数字组合的列表,并逐行打印它们,但不要包含括号。以下是我迄今为止编写的代码。
import itertools
x = range(1, 4)
y = list(itertools.combinations(x, 2))
combos = []
for i in range(len(y)):
combos.append(y[i])
print(combos[i])
输出是:
1, 2
1, 3
2, 3
我需要它以不包含括号的方式打印输出。我已经寻找了各种解决方案,但迄今为止都没有找到任何东西。任何帮助将不胜感激。
英文:
I want to make a list of all possible combinations in a range of numbers, and print them line by line without surrounding parentheses. Here is the code I've built so far.
import itertools
x = range(1,4
y = list(itertools.combinations(x,2))
combos = []
for i in range(len(y)):
combos.append(y[i])
print(combos[i])
The output is
(1,2)
(1,3)
(2,3)
I need it to print the output without surrounding parentheses. I've looked high and low for a solution, but so far I can't find anything. Any help would be much appreciated.
答案1
得分: 1
这是一个简单的方法来做到这一点。
import itertools
x = range(1, 4)
y = list(itertools.combinations(x, 2))
for pair in y:
print(*pair)
如果你真的想要分隔逗号,你可以这样做:
import itertools
x = range(1, 4)
y = list(itertools.combinations(x, 2))
for pair in y:
print(f"{pair[0]},{pair[1]}")
英文:
Here's a simple way to do this.
import itertools
x = range(1,4)
y = list(itertools.combinations(x,2))
for pair in y:
print(*pair)
If you really want the separating commas, you could do this:
import itertools
x = range(1,4)
y = list(itertools.combinations(x,2))
for pair in y:
print(f"{pair[0]},{pair[1]}")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论