Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从带有公共分隔符的字符串列表生成组合列表,而不使用反向重复项_Python_List_Duplicates_Combinations - Fatal编程技术网

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

Python 从带有公共分隔符的字符串列表生成组合列表,而不使用反向重复项,python,list,duplicates,combinations,Python,List,Duplicates,Combinations,我正在尝试使用下面的代码从带有公共分隔符的字符串列表生成组合列表,而不使用反向重复: separator = "*"; VarList = ["y","lp","ep","rmp","cmp","cp","fp"] newVarList = []; currPosition = 0 for currVar in VarList: currPosition +=1 nextPosition = 0 for nextVar in VarList: nex

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

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)))

我已经成功地生成了这些组合,并在没有括号或引号的情况下列出了它们,但反向重复项仍然存在。任何建议都将不胜感激。

请确保当前位置始终小于下一个位置: 也许通过改变

if currPosition != nextPosition:

如果当前位置

这将删除重复项。

@wim,例如AB和BA。您认为
“cpm”
“cmp”
的“反向重复”吗?如果是这样的话,你是在寻找组合而不是排列。如果没有,请让我们更清楚地知道。@RoryDaulton道歉,你是对的;应该是组合。这正是我出错的地方,现在效果很好。
if currPosition < nextPosition: