Warthog1
Warthog1

Reputation: 123

Python removing commas without using replace

I have a python output that is all permutations of words that are entered into a tKinter GUI. I need the commmas taken out of the output. I have used replace, but this ends up with a whitespace after every permutation.

def exactMatch(entries):
     words = [entry[1].get() for entry in entries]
     perms = [p for p in permutations((words))]
     x1 = str(perms)

     perms2 = x1.replace("," , '') 
     perms3 = perms2.replace("'" , '') #Takes out the Quotations
     perms4 = perms3.replace("(" , '[')
     perms5 = perms4.replace(')' , ']\n')
     perms6 = perms5.replace (" ", "")
     print(perms6)


"a b c hello"
 "a b hello c"
 "a c b hello"
 "a c hello b"
 "a hello b c"
 "a hello c b"
 "b a c hello"
 "b a hello c"
 "b c a hello"
 "b c hello a"
 "b hello a c"

As you can see above, every permutation output had a comma which I have replaced with a white space. My goal is to not use white spaces, and instead take the commas out so that all of the outputs line up with the first line of the output. How would I go about doing this? Any help is appreciated.

Upvotes: 0

Views: 155

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799082

Iterate over the result.

for perm in perms:
  print(perm)

Upvotes: 2

bhansa
bhansa

Reputation: 7514

One other way:

for perm in perms:
    print(*perm)

where perms is of type <class 'itertools.permutations'>

Upvotes: 1

Related Questions