I’ve searched, but found nothing to help.. This is an example:
List.txt
a
b
c
d
I want to be able to get an output like this:
Output.txt
ab
ac
ad
ba
bc
bd
ca
cb
cd
etc...
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Pretty straightforward…
Output:
Some notes:
The
withstatement ensures the file will be closed when you’re done with it.lettersis a generator expression, which in many cases (though not this one) will save you from having to read the entire file in at once.The uses of
l.strip()are meant to nicely handle accidental blank lines if present in input.itertools.permutationsis correct, NOTitertools.combinationswhich considersab==baand will not include the latter as output.Happy pythoning 🙂