我知道這與以前的問題類似,但我的請求中存在足夠的差異以值得提出新問題。我有一個字符串元素的列表。生成列表元素對的每個置換而不重複或倒置對
>>> 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