感谢大家的帮助。我认为我的查询缺乏详细信息。我希望得到一个表格结果,其中每一行都是每个人/字符串单独与另一个字符串配对的彻底结果。
我不擅长编码,所以我受到启发在 python 中使用递归函数和一点数学魔法来完成它。我能够让它接受一个变量列表并相应地扩展。
nameList = ['1', '2', '3', '4', '5']
if(len(nameList) % 2 == 1):
nameList.append(" ")
print(nameList)
length = len(nameList)
possible = 1
while length > 1:
possible = possible*(length-1)
length -= 2
def completeCombinations (List, line):
BaseList = list(List)
if (line % (len(List)-1) == 0):
num = (len(List)-1)
else:
num = line % (len(List)-1)
BaseList.remove(List[0])
BaseList.remove(List[num])
if(len(List) > 2):
CurrentList = list(completeCombinations(BaseList, line))
CurrentList.insert(0, List[num])
CurrentList.insert(0, List[0])
else:
CurrentList = list(List)
return CurrentList
i = 1
while i < possible:
print completeCombinations(nameList, i+1)
i += 1
结果看起来非常接近我的预期,如果不一定漂亮的话。
['1', '2', '3', '4', '5', ' ']
['1', '3', '2', '5', '4', ' ']
['1', '4', '2', ' ', '3', '5']
['1', '5', '2', '3', '4', ' ']
['1', ' ', '2', '4', '3', '5']
['1', '2', '3', ' ', '4', '5']
['1', '3', '2', '4', '5', ' ']
['1', '4', '2', '5', '3', ' ']
['1', '5', '2', ' ', '3', '4']
['1', ' ', '2', '3', '4', '5']
['1', '2', '3', '5', '4', ' ']
['1', '3', '2', ' ', '4', '5']
['1', '4', '2', '3', '5', ' ']
['1', '5', '2', '4', '3', ' ']
['1', ' ', '2', '5', '3', '4']
衷心感谢任何可以帮助我完善它并将结果导入谷歌表格的人