Python simple Code

Denis McMahon denismfmcmahon at gmail.com
Wed Jan 28 04:15:32 CET 2015


On Tue, 27 Jan 2015 15:22:01 -0800, Salem Alqahtani wrote:

> I appreciate your answers and the output that I am expected from my
> simple code is the following:
> 
> ['salem','Ali','sultan']
> ['salem','sultan','Ali']
> ['Ali','sultan','salem']
> ['Ali','salem','sultan']
> ['sultan','Ali','salem']
> ['sultan','salem','sultan']

A long way to do this is as follows (you've already been shown the short 
way using permutations)

words = ['salem','Ali','sultan']

for a in words:
    for b in words:
         if b != a:
              for c in words:
                  if c != a:
                      if c != b:
                          print [a, b, c]

Not quite so long:

words = ['salem','Ali','sultan']

for a in words:
    for b in [x for x in words if x != a]:
        for c in [x for x in words if x != a and x != b]:
            print [a, b, c]

Observe that neither of these scale as elegantly as the permutations 
example already given.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list