我知道这与以前的问题类似,但我的请求中存在足够的差异以值得提出新问题。我有一个字符串元素的列表。生成列表元素对的每个置换而不重复或倒置对
>>> mainlist
['one', 'two', 'three', 'four', 'five']
我想创建一个环形程序,它的第一个元素,然后用剩余的元素,像这样对吧:
['one two', 'one three', 'one four', 'one five']
注意到它没有创造对'one one'
的下一个周期应为:
['two three', 'two, four', 'two five']
请注意,它没有根据我的目的,创建'two two'
甚至'two one'
,这相当于'one two'
。
等等...
我得最近的是:
for primary in mainlist:
for secondary in mainlist:
if primary == secondary: print("skipping...")
else: print(primary + " " + secondary)
>> skipping...
one two
one three
one four
one five
two one
skipping...
two three
two four
two five
three one
three two
skipping...
three four
three five
four one
four two
four three
skipping...
four five
five one
five two
five three
five four
skipping...
基于上述可以看到,这不完全匹配我后。任何帮助将非常感激 - 我敢肯定,有一个优雅的解决方案在那里。
谢谢 - 我会使用它,因为它是在我现有的代码中最容易实现的 – Beeman