input example 1:[2, 1]
output example 1:[[1, 2], [2, 1]]
input example 2:[1, 2, 3]
output example 2:[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
아마 해당 기능을 모르신다면 for문을 이용해 리스트를 만들어 갈 것이라 예상돼요!
defsolution(mylist): result= [mylist[:]] c= [0] *len(mylist) i=0whilei<len(mylist): ifc[i] <i: ifi%2==0: mylist[0], mylist[i] =mylist[i], mylist[0] else: mylist[c[i]], mylist[i] =mylist[i], mylist[c[i]] result.append(mylist[:]) c[i] +=1i=0else: c[i] =0i+=1returnresult
파이썬의 itertools에는 permutations라는 함수가 있어요!
importitertoolsdefsolution(mylist): mylist.sort() returnlist(map(list, itertools.permutations(mylist)))
아주 간단하게 구현이 되는군요!
정말 우리가 생각하는 것 이상의 기능을 가진 함수들이 많은 것 같아요.