从带有公共分隔符的字符串列表生成组合列表,而不使用反向重复

2024-09-24 22:31:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用下面的代码从带有公共分隔符的字符串列表生成组合列表,而不使用反向重复:

separator = "*";
VarList = ["y","lp","ep","rmp","cmp","cp","fp"]


newVarList = [];

currPosition = 0
for currVar in VarList:
    currPosition +=1
    nextPosition = 0
    for nextVar in VarList:
        nextPosition+=1
        if currPosition != nextPosition:
            currText = currVar + separator + nextVar
            if currText not in newVarList:
                newVarList.append(currText)

print len(newVarList)
print(' '.join(map(str, newVarList)))

我已经设法产生了组合,并有他们列出没有括号或引号,但反向重复仍然存在。任何建议都将不胜感激


Tags: 代码in列表forifprint分隔符separator