【发布时间】:2020-12-21 18:40:41
【问题描述】:
我正在尝试制作一个以 X 为前缀并以 Y 为后缀的排列列表。 以下是我尝试过的一些示例。
perm = permutations ([1,2,3], 3)
for i in list(perm):
print ("Adam" + (i) + "Bob")
这会带回错误“TypeError: can only concatenate str (not "tuple") to str”
perm = permutations ([1,2,3], 3)
for i in list(perm):
list (i)
def blink():
print (("X" + (i) + "Y"))
blink()
这会带回错误“TypeError: can only concatenate str (not "tuple") to str”
理想情况下,最终结果会打印出来
X(1, 2, 3)Y
X(1, 3, 2)Y
X(2, 1, 3)Y
X(2, 3, 1)Y
X(3, 1, 2)Y
X(3, 2, 1)Y
【问题讨论】:
-
旁注:这里没有理由做
for i in list(perm):;permutations已经返回了一个迭代器,而list在迭代之前立即对其进行化仅意味着您必须在只需要一个时使用所有tuples 制作一个(可能很昂贵,尽管在这种情况下不是)临时的tuples一次。
标签: python tuples permutation