একটি প্রদত্ত স্ট্রিং এর সমস্ত সম্ভাব্য স্থানান্তর খুঁজে পেতে, আপনি itertools মডিউলটি ব্যবহার করতে পারেন যার একটি কার্যকর পদ্ধতি রয়েছে যাকে বলা হয় permutations(iterable[, r])। এই পদ্ধতি টিপল হিসাবে পুনরাবৃত্তিযোগ্য উপাদানগুলির ক্রমাগত r দৈর্ঘ্যের স্থানান্তর প্রদান করে।
স্ট্রিং হিসাবে সমস্ত পারমুটেশন পেতে, আপনাকে ফাংশন কলে পুনরাবৃত্তি করতে হবে এবং টিপলে যোগ দিতে হবে। যেমন:
>>>from itertools import permutations
>>>print [''.join(p) for p in permutations('dune')]
['dune','duen', 'dnue', 'dneu', 'deun', 'denu', 'udne', 'uden', 'unde', 'uned', 'uedn','uend', 'ndue', 'ndeu', 'nude',
'nued', 'nedu', 'neud', 'edun', 'ednu','eudn', 'eund', 'endu', 'enud'] আপনি যদি বিল্ট পদ্ধতিতে ব্যবহার করতে না চান তবে আপনার নিজের তৈরি করতে চান, আপনি নিম্নলিখিত পুনরাবৃত্তিমূলক সমাধান ব্যবহার করতে পারেন:
def permutations(string, step = 0):
if step == len(string):
# we've gotten to the end, print the permutation
print "".join(string)
for i in range(step, len(string)):
# copy the string (store as array)
string_copy = [c for c in string]
# swap the current index with the step
string_copy[step], string_copy[i] =string_copy[i], string_copy[step]
# recurse on the portion of the stringthat has not been swapped yet
permutations(string_copy, step + 1)
print (permutations ('one')) আউটপুট
one oen noe neo eno eon None