[英]Is there a function to create a list of unique elements in unique index using python?
有沒有辦法創建一組新的元素列表,原始列表的每個元素都在以下列表中的唯一索引中?
orginal_list=['r', 'g', 'b', 'y']
output: ['y', 'g', 'r', 'b'],['g', 'y', 'b', 'r'],['r', 'b', 'y', 'g'],['b', 'r', 'g', 'y']
or
output: ['y', 'r', 'b', 'g'],['g', 'y', 'r', 'b'],['r', 'b', 'g', 'y'],['b', 'g', 'y', 'r']
or
...
我曾嘗試使用iterators.permutations
,但這不符合唯一索引要求。
最簡單的方法是輪換列表。 這是一個簡單的生成器 function 產生所有這樣的旋轉:
def rots(lst):
for i in range(len(lst)):
yield lst[i:] + lst[:i]
>>> list(rots(['r', 'g', 'b', 'y']))
[['r', 'g', 'b', 'y'],
['g', 'b', 'y', 'r'],
['b', 'y', 'r', 'g'],
['y', 'r', 'g', 'b']]
旋轉保證每個元素在每個索引中實際出現一次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.